简体   繁体   中英

C# initialize class

I am creating wpf application in vs2010. I have a simple question.

I have a class to display image:

private void displayImage()
{
     newImage.Width = 900;
     newImage.Height = 700;

     BitmapImage testim = new BitmapImage();
     testim.BeginInit();
     testim.UriSource = new Uri("E:\\WpfApplication1\\Images\\test.jpg");
     testim.EndInit();

     newImage.Source = testim;
}

XAML:

<ScrollViewer Name="MyScrollViewer" CanContentScroll="True" HorizontalScrollBarVisibility="Visible" Grid.Row="1" Grid.Column="1" >
    <Image Name="newImage" ImageFailed="newImage_ImageFailed" HorizontalAlignment="Right" Margin="10" />
</ScrollViewer>

To initialize it, I call displayImage(); from another class.

I want this image to be visible as soon as I run the application. I want to how to initialize it without calling it from another class?

Thanks a lot.

regards

I'm unsure why you try to set it in code. It is easier to set this in XAML:

<ScrollViewer Name="MyScrollViewer" CanContentScroll="True" HorizontalScrollBarVisibility="Visible" Grid.Row="1" Grid.Column="1" >
    <Image Name="newImage" ImageFailed="newImage_ImageFailed" 
                           HorizontalAlignment="Right" Width="900" 
                           Height="700" Margin="10" Source="/Images/test.jpg" />
</ScrollViewer>

The above should already do the trick. Note that I've also set the Width and Height values for the image. No code is needed to achieve this.

And if you really want to set the image in the code on start, you need to be in the constructor of the WPF page/window. The constructor of a class gets called when the class is being created. In this case when the Window or Page needs to be created, the first thing your application does is calling the constructor. In this constructor, the application also learns that some graphical stuff has to be built. That's how constructors work.

After InitializeComponent(); , you can put in whatever you like. For example:

public partial class MainWindow : Page
{
    public MainWindow()
    {
        InitializeComponent();
        displayImage();
    }

    private void displayImage()
    {
        newImage.Width = 900;
        newImage.Height = 700;

        BitmapImage testim = new BitmapImage();
        testim.BeginInit();
        testim.UriSource = new Uri("E:\\WpfApplication1\\Images\\test.jpg");
        testim.EndInit();

        newImage.Source = testim;
    }
}

If the path is going to be dynamic, binding your image is the best practice. In your XAML, have something like:

<Image>
    <Image.Source>
        <BitmapImage UriSource="{Binding Path=ImagePath}" />
    </Image.Source>
</Image>

So the UriSource gets bound to your ImagePath . In the code, have the following:

public BitmapImage ImagePath
{ get { return testim; } }

So it gets bound to that. If you change the value, the binding makes sure the value in the GUI changes with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM