[英]How to bind XAML elements to App.XAML.CS as global variables?
我想從不同的類別(頁面)更改圖像源,包括彈出菜單
我被承認我必須使圖像成為全局變量。 但是不知道該怎么做。 還有其他辦法嗎?
由於您希望更改影響所有頁面,因此共享視圖模型是最佳選擇。 首先創建您的視圖模型類:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace App.ViewModel
{
public sealed class MyViewModel : INotifyPropertyChanged
{
#region BindableBase implementation
/// <summary>
/// Multicast event for property change notifications.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Checks if a property already matches a desired value. Sets the property and
/// notifies listeners only when necessary.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to a property with both getter and setter.</param>
/// <param name="value">Desired value for the property.</param>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers that
/// support CallerMemberName.</param>
/// <returns>True if the value was changed, false if the existing value matched the
/// desired value.</returns>
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
/// <summary>
/// Notifies listeners that a property value has changed.
/// </summary>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers
/// that support <see cref="CallerMemberNameAttribute"/>.</param>
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region MyImage
/// <summary>
/// Backing field for the MyImage property.
/// </summary>
private ImageSource myImage;
/// <summary>
/// Gets or sets a value indicating ....
/// </summary>
public string MyImage
{
get { return this.myImage; }
set { this.SetProperty(ref this.myImage, value); }
}
#endregion
}
}
然后在App.xaml中將其實例化為資源:
<Application
x:Class="App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="using:App.ViewModel">
<Application.Resources>
<viewModel:MyViewModel
x:Key="MyViewModel"/>
</Application.Resources>
</Application>
然后,您要使用該圖像的任何地方都應放置以下內容:
<Image
Source="{Binding MyImage, Source={StaticResource MyViewModel}"/>
然后,當您要更改圖像時,可以執行以下操作:
((MyViewModel)App.Current.Resources["MyViewModel"]).MyImage = new BitmapImage();
您是否正在使用ViewModel
類來ViewModel
代碼?
如果是,則可以創建BaseViewModel
類,然后可以在其中放置全局屬性。
並且所有viewmodel類都必須繼承BaseViewModel。 這樣,從任何視圖模型中,您都可以通過訪問基類屬性來更改或設置Image源。
Public class BaseViewModel
{
//place your global properties here
Public string Name {get;set;}
}
Public class ViewModel1:BaseViewModel
{
//you can access the global property here
void Method1()
{
Name="something";
}
}
Public class ViewModel2:BaseViewModel
{
//you can access the global property here
void Method2()
{
Name="something";
}
}
希望這可以幫助。
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.