Share static properties for multiple windows in WPF and static properties for windows in wpf

Source: Internet
Author: User

Share static properties for multiple windows in WPF and static properties for windows in wpf

Because my DoubanFm needs to set a global CurrentSong after rethinking, this field should be known to all VMS, and I want to use it as the common attribute of all my VMS at the same time. In addition, I want to minimize code duplication and improve reuse. So I did the following: (below is my test in WPF)

First, we need to familiarize ourselves with the MVVMlight framework.

Then:

(1) Base Class 1. Abstract VM base class
abstract class MainViewModel : ViewModelBase

 

II. Private Static, public non-static exposure, and key points: Use RaisePropertyChanged <T> to send the change through the Messenger message after the property is changed.
 1 protected const string DataItemPropertyName = "DataItem"; 2  3         static private DataItem dataitem=null; 4  5  6          public DataItem DataItem 7         { 8             get 9             {10                 return dataitem;11             }12 13             set14             {15                 if (dataitem == value)16                 {17                     return;18                 }19 20              var oldvalue=dataitem;21                 dataitem = value;22                 RaisePropertyChanged<DataItem>(DataItemPropertyName, oldvalue, value, true);23             }24         }
3. Provides common message registration methods for subclass VMS
 1          protected void RegisterVM(object vm) 2          { 3              Messenger.Default.Register<PropertyChangedMessage<DataItem>>(vm, 4                  (message) => 5                  { 6                      if(message.PropertyName==DataItemPropertyName) 7                      RaisePropertyChanged(DataItemPropertyName); 8                   9                  });10          11          }
4. The constructor only assigns values to static fields as needed (in this example, only null static values are assigned)
1 protected MainViewModel (IDataService dataService) 2 {3 _ dataService = dataService; 4 _ dataService. getData (5 (item, error) => 6 {7 if (dataitem = null) 8 {9 dataitem = item; // initialize static variable 10} 11}); 12}
(2) subclass:

Subclass 1

1  public ViewModel1(IDataService  dd):base(dd)2         {3             RegisterVM(this);4         }

Subclass 2

1     public class ViewModel2 :MainViewModel2     {3 4         5       public ViewModel2(IDataService dd):base(dd)6         {7             RegisterVM(this);8         }9     }

Subclass 3

1     public class ViewModel3 : MainViewModel2     {3 4         public ViewModel3(IDataService dd)5             : base(dd)6         {7             RegisterVM(this);8         }9     }

Register a subclass of Ioc

 SimpleIoc.Default.Register<ViewModel1>();            SimpleIoc.Default.Register<ViewModel2>();            SimpleIoc.Default.Register<ViewModel3>();

 

(3) Effect

Original page

 

After the attribute changes

 

 

 

Download the source code.

 


The difference between wpf dependency attributes and static attributes

First, let's talk about common attributes. Each instance has a backup in memory. A static attribute has only one class. No matter how many instances there are, there is only one static attribute with the same value. There is only one backup in the application domain. The dependency attributes are different. Only when the dependency attribute of an instance is assigned a value, this attribute of the instance is backed up in the memory. For example, you have 1000 yuan, which is stored in a bank. It does not belong to you, but it still belongs to you, not to the bank or other people.

How does aspnet control the page range of static (shared) attributes?

Use the example protected int XXX {In ViewState {
Get
{
Object o = ViewState ["xxx"];
If (o! = Null)
Return Convert. ToInt32 (o );
Else
Return 0;
}
Set
{
ViewState ["xxx"] = value;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.