如何在 UWP 中更新页面上的图像?
How to update an image on a page in UWP?
我有一个 GridviewItem。这个 GridviewItem 有一个背景,它是一个 ImageBrush。现在我想在单击某个按钮时将此 ImageBrush 更改为新源。
为此我使用:
blck.Background = new ImageBrush(new BitmapImage(new Uri("ms-appx:///Assets/SensorBG.png")));
它确实有效,但只有在我单击相应的 GridviewItem 时才会显示新图像。谁能告诉我如何在不单击 GridviewItem 的情况下更新它?
我已经尝试将它放入此块中,但没有成功:
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
blck.Background = new ImageBrush(new BitmapImage(new Uri("ms-appx:///Assets/SensorBG.png")));
}
);
最好的情况是,如果您已经定义了具有合适属性的 ItemClass 并实现了 INotifyPropertyChanged - 通过适当的绑定,每个更改都会更新 UI。这是一个小样本 - XAML:
<StackPanel>
<Button Content="Change background of second item" Click="Button_Click"/>
<GridView Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" ItemsSource="{x:Bind Items}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:ItemClass">
<Border>
<Border.Background>
<ImageBrush ImageSource="{x:Bind Image, Mode=OneWay}"/>
</Border.Background>
<TextBlock Text="{x:Bind Name}"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
以及后面的代码:
public sealed partial class MainPage : Page
{
public List<ItemClass> Items = new List<ItemClass>();
public MainPage()
{
Items.Add(new ItemClass { Name = "First item", Image = new BitmapImage(new Uri("ms-appx:///Assets/StoreLogo.png")) });
Items.Add(new ItemClass { Name = "Second item", Image = new BitmapImage(new Uri("ms-appx:///Assets/StoreLogo.png")) });
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) => Items[1].Image = new BitmapImage(new Uri("ms-appx:///test.jpg"));
}
public class ItemClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private ImageSource image;
public ImageSource Image
{
get { return image; }
set { image = value; RaiseProperty(nameof(Image)); }
}
public string Name { get; set; }
}
我有一个 GridviewItem。这个 GridviewItem 有一个背景,它是一个 ImageBrush。现在我想在单击某个按钮时将此 ImageBrush 更改为新源。
为此我使用:
blck.Background = new ImageBrush(new BitmapImage(new Uri("ms-appx:///Assets/SensorBG.png")));
它确实有效,但只有在我单击相应的 GridviewItem 时才会显示新图像。谁能告诉我如何在不单击 GridviewItem 的情况下更新它?
我已经尝试将它放入此块中,但没有成功:
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
blck.Background = new ImageBrush(new BitmapImage(new Uri("ms-appx:///Assets/SensorBG.png")));
}
);
最好的情况是,如果您已经定义了具有合适属性的 ItemClass 并实现了 INotifyPropertyChanged - 通过适当的绑定,每个更改都会更新 UI。这是一个小样本 - XAML:
<StackPanel>
<Button Content="Change background of second item" Click="Button_Click"/>
<GridView Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" ItemsSource="{x:Bind Items}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:ItemClass">
<Border>
<Border.Background>
<ImageBrush ImageSource="{x:Bind Image, Mode=OneWay}"/>
</Border.Background>
<TextBlock Text="{x:Bind Name}"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
以及后面的代码:
public sealed partial class MainPage : Page
{
public List<ItemClass> Items = new List<ItemClass>();
public MainPage()
{
Items.Add(new ItemClass { Name = "First item", Image = new BitmapImage(new Uri("ms-appx:///Assets/StoreLogo.png")) });
Items.Add(new ItemClass { Name = "Second item", Image = new BitmapImage(new Uri("ms-appx:///Assets/StoreLogo.png")) });
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) => Items[1].Image = new BitmapImage(new Uri("ms-appx:///test.jpg"));
}
public class ItemClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private ImageSource image;
public ImageSource Image
{
get { return image; }
set { image = value; RaiseProperty(nameof(Image)); }
}
public string Name { get; set; }
}