XAML上で配置したImageにボタン押下をトリガーに画像を表示し、その後メッセージボックスを出すという単純なプログラムを作成しました。
挙動確認した所、表示されたメッセージボックスを閉じた際に、タイトルに記載した奇妙な現象がImage上に発生しました。
メッセージボックスを少し動かした後に閉じると、この現象は発生しません。
何が原因でこのような現象になったのかご教授お願い致します。
※実際に現象が起きたソースコードも載せます。
###該当のソースコード
MainWindow.xaml
1<Window x:Class="WpfTest.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:local="clr-namespace:WpfTest" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 Title="MainWindow" 8 Width="300" 9 Height="300" 10 mc:Ignorable="d"> 11 <Grid> 12 <Grid Width="150" 13 Height="300" 14 HorizontalAlignment="Left" 15 VerticalAlignment="Top" 16 Background="DimGray"> 17 <Image x:Name="image" 18 Margin="10,10,10,10" 19 HorizontalAlignment="Left" 20 VerticalAlignment="Top" 21 Stretch="None" /> 22 </Grid> 23 <Button x:Name="button" 24 Width="75" 25 Margin="155,10,0,0" 26 HorizontalAlignment="Left" 27 VerticalAlignment="Top" 28 Content="Button" /> 29 30 </Grid> 31</Window>
MainWindow.xaml.cs
1using System; 2using System.Collections.Generic; 3using System.IO; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7using System.Windows; 8using System.Windows.Controls; 9using System.Windows.Data; 10using System.Windows.Documents; 11using System.Windows.Input; 12using System.Windows.Media; 13using System.Windows.Media.Imaging; 14using System.Windows.Navigation; 15using System.Windows.Shapes; 16using System.Windows.Threading; 17 18namespace WpfTest 19{ 20 public static class ExtensionMethods 21 { 22 private static Action EmptyDelegate = delegate () { }; 23 public static void Refresh(this UIElement uiElement) 24 { 25 uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate); 26 } 27 } 28 29 /// <summary> 30 /// MainWindow.xaml の相互作用ロジック 31 /// </summary> 32 public partial class MainWindow : Window 33 { 34 public MainWindow() 35 { 36 InitializeComponent(); 37 38 WindowStartupLocation = WindowStartupLocation.CenterScreen; 39 40 button.Click += new RoutedEventHandler(OnClick); 41 } 42 43 private void OnClick(object sender, RoutedEventArgs e) 44 { 45 string path = @"C:\WpfTest\WpfTest\img\test.png"; 46 47 //画像描画 48 BitmapImage wkBmp = new BitmapImage(); 49 FileStream fs = File.OpenRead(path); ; 50 wkBmp.BeginInit(); 51 wkBmp.CacheOption = BitmapCacheOption.OnLoad; 52 wkBmp.CreateOptions = BitmapCreateOptions.None; 53 wkBmp.StreamSource = fs; 54 wkBmp.EndInit(); 55 wkBmp.Freeze(); 56 fs.Dispose(); 57 58 image.Source = wkBmp; 59 60 MessageBox.Show("テスト"); 61 62 //this.Refresh(); //全体(効果なし) 63 //image.Refresh(); //個別(効果なし) 64 } 65 66 } 67}
###補足情報(言語/FW/ツール等のバージョンなど)
Windows7 Pro 64bit
visual studio 2015 pro
.NETフレームワーク 4.5.2
テーマ:Windows7 ベーシック
画像:800*600の全て黒で塗りつぶしたpng画像(paintで作成)
回答2件
あなたの回答
tips
プレビュー