使用 INotifyPropertyChanged 从 BitmapImage 变量绑定 Image.Source
Binding Image.Source from BitmapImage variable using INotifyPropertyChanged
我想将我的 CurrentWallpaper 变量绑定到 Image.Source 并在图像控件更改时更新它。
那是我的代码:
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public BitmapImage CurrWall = new BitmapImage();
BitmapImage CurrentWallpaper
{
get { return CurrWall; }
set { CurrWall = value; OnPropertyChanged("CurrentWallpaper"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我在页面标记中添加了这个 DataContext 属性。
DataContext="{Binding RelativeSource={RelativeSource Self}}"
那是我的图像控件
<Image x:Name="CurrentWallpaperImage" Source="{x:Bind CurrWall}"/>
当我尝试设置新源时,图像控件没有更新。
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(path);
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
await CurrentWallpaper.SetSourceAsync(stream);
首先声明CurrentWallpaper
为public
属性
private BitmapImage currentWall = new BitmapImage();
public BitmapImage CurrentWallpaper
{
get { return currentWall; }
set { currentWall= value; OnPropertyChanged("CurrentWallpaper"); }
}
第二个,将Binding
改为OneWay
<Image x:Name="CurrentWallpaperImage" Source="{x:Bind CurrWall, Mode=OneWay}"/>
如果在第一步或第二步后仍不起作用,请尝试手动引发事件
await CurrentWallpaper.SetSourceAsync(stream);
OnPropertyChanged("CurrentWallpaper");
您实际上并没有为页面的 CurrentWallpaper 属性 分配新值。因此不会触发 OnPropertyChanged。
2 个选项,或者您仍然使用 SetSource 方法,但在该行之后自己调用 OnPropertyChanged("CurrentWallpaper");
以确保更新 UI。
或者使用您拥有的流创建一个新的 BitmapImage,并将该新的位图图像完全分配给您的 CurrentWallpaper。
CurrentWallpaper = newBitmapImage;
我想将我的 CurrentWallpaper 变量绑定到 Image.Source 并在图像控件更改时更新它。 那是我的代码:
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public BitmapImage CurrWall = new BitmapImage();
BitmapImage CurrentWallpaper
{
get { return CurrWall; }
set { CurrWall = value; OnPropertyChanged("CurrentWallpaper"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我在页面标记中添加了这个 DataContext 属性。
DataContext="{Binding RelativeSource={RelativeSource Self}}"
那是我的图像控件
<Image x:Name="CurrentWallpaperImage" Source="{x:Bind CurrWall}"/>
当我尝试设置新源时,图像控件没有更新。
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(path);
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
await CurrentWallpaper.SetSourceAsync(stream);
首先声明CurrentWallpaper
为public
属性
private BitmapImage currentWall = new BitmapImage();
public BitmapImage CurrentWallpaper
{
get { return currentWall; }
set { currentWall= value; OnPropertyChanged("CurrentWallpaper"); }
}
第二个,将Binding
改为OneWay
<Image x:Name="CurrentWallpaperImage" Source="{x:Bind CurrWall, Mode=OneWay}"/>
如果在第一步或第二步后仍不起作用,请尝试手动引发事件
await CurrentWallpaper.SetSourceAsync(stream);
OnPropertyChanged("CurrentWallpaper");
您实际上并没有为页面的 CurrentWallpaper 属性 分配新值。因此不会触发 OnPropertyChanged。
2 个选项,或者您仍然使用 SetSource 方法,但在该行之后自己调用 OnPropertyChanged("CurrentWallpaper");
以确保更新 UI。
或者使用您拥有的流创建一个新的 BitmapImage,并将该新的位图图像完全分配给您的 CurrentWallpaper。
CurrentWallpaper = newBitmapImage;