简体   繁体   English

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

[英]How to bind XAML elements to App.XAML.CS as global variables?

I want to change an image source from different classes (pages) including a settings flyout 我想从不同的类别(页面)更改图像源,包括弹出菜单

I was acknowledged that i have to make the image as a global variable. 我被承认我必须使图像成为全局变量。 But couldn't figure out how to do it. 但是不知道该怎么做。 Is there any other way ? 还有其他办法吗?

Since you want a change to affect all pages - a shared view model is your best option. 由于您希望更改影响所有页面,因此共享视图模型是最佳选择。 Create your view model class first: 首先创建您的视图模型类:

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
    }
}

Then in App.xaml instantiate it as a resource: 然后在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>

Then anywhere you want to use that image put something like: 然后,您要使用该图像的任何地方都应放置以下内容:

<Image
    Source="{Binding MyImage, Source={StaticResource MyViewModel}"/>

Then when you want to change the image you can do something like this: 然后,当您要更改图像时,可以执行以下操作:

((MyViewModel)App.Current.Resources["MyViewModel"]).MyImage = new BitmapImage();

Are you using ViewModel classes to do your codes? 您是否正在使用ViewModel类来ViewModel代码?
If yes, you can create a BaseViewModel class and there you can place your global property. 如果是,则可以创建BaseViewModel类,然后可以在其中放置全局属性。

And all your viewmodel classes must have to inherit BaseViewModel. 并且所有viewmodel类都必须继承BaseViewModel。 So that from any viewmodel you can change or set the Image source by accessing the base class property. 这样,从任何视图模型中,您都可以通过访问基类属性来更改或设置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";
   }
}

Hope this helps. 希望这可以帮助。
Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM