如何将XAML元素绑定到App.XAML.CS作为全局变量?

时间:2014-05-01 08:50:06

标签: c# windows xaml windows-runtime winrt-xaml

我想从不同的类(页面)更改图像源,包括设置弹出

我承认我必须将图像作为全局变量。但无法弄清楚如何做到这一点。还有其他办法吗?

2 个答案:

答案 0 :(得分:5)

由于您希望更改影响所有页面 - 共享视图模型是您的最佳选择。首先创建视图模型类:

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();

答案 1 :(得分:1)

您是否使用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";
   }
}

希望这有帮助。
感谢