How to switch images in a C# WPF application?

后端 未结 2 1838
抹茶落季
抹茶落季 2021-01-24 10:21

I am trying to make an application that switches between an image of the heads sign of a coin and the tails side of a coin. However, every time I press either the \"heads\" butt

相关标签:
2条回答
  • 2021-01-24 10:59

    Clemens is absolutely right, and his second alternative is far superior because it doesn't re-load the bitmaps each time you flip them. However if I may suggest an even better alternative (IMHO) to what you're doing, instead of changing the Source of the coinImage each time, you might instead want to have two Images, for example, coinHeadsImage and coinTailsImage, and flip their respective Visibility properties in those Click handlers. Wrap both Images in their own common Grid so that they are overlapping in the visual tree. I'm not 100% certain, but I believe changing the Visibility of the Images would be more efficient speed-wise than setting the Source property, and either way, it would be better architecture because you could bind the Visibility properties directly to a hypothetical IsHeads property in your code-behind or view model, using appropriate converters of course.

    Also, any time you use the as syntax, you generally should check the result for null. Unlike a simple type cast, you won't get an exception if the object cannot covert to the desired type when you use as. Had you checked for null you would have caught your error there.

    0 讨论(0)
  • 2021-01-24 11:09

    You can't use the sender argument, because that's the Button, not the Image control.

    Use the coinImage member instead:

    private void headsButton_Click(object sender, RoutedEventArgs e)
    {
        coinImage.Source = new BitmapImage(new Uri(@"C:\Users\Raymond Karrenbauer\Documents\Visual Studio 2015\Projects\HeadsOrTails\heads.jpg"));
    }
    
    private void tailsButton_Click(object sender, RoutedEventArgs e)
    {
        coinImage.Source = new BitmapImage(new Uri(@"C:\Users\Raymond Karrenbauer\Documents\Visual Studio 2015\Projects\HeadsOrTails\tails.jpg"));
    }
    

    Besides that, you should add both image files to your Visual Studio project, set their Build Action to Resource and access them by a Resource File Pack URI. This way you wouldn't have to deal with absolute file paths:

    private void headsButton_Click(object sender, RoutedEventArgs e)
    {
        coinImage.Source = new BitmapImage(new Uri("pack://application:,,,/heads.jpg"));
    }
    
    private void tailsButton_Click(object sender, RoutedEventArgs e)
    {
        coinImage.Source = new BitmapImage(new Uri("pack://application:,,,/tails.jpg"));
    }
    

    You may then also add the BitmapImages as XAML Resources:

    <Window ...>
        <Window.Resources>
            <BitmapImage x:Key="heads" UriSource="heads.png"/>
            <BitmapImage x:Key="tails" UriSource="tails.png"/>
        </Window.Resources>
        ...
    </Window>
    

    And use them like this:

    private void headsButton_Click(object sender, RoutedEventArgs e)
    {
        coinImage.Source = (ImageSource)Resources["heads"];
    }
    
    private void tailsButton_Click(object sender, RoutedEventArgs e)
    {
        coinImage.Source = (ImageSource)Resources["tails"];
    }
    
    0 讨论(0)
提交回复
热议问题