1. 程式人生 > >記錄一個比較有意思的程式碼 關於Static 和資料繫結

記錄一個比較有意思的程式碼 關於Static 和資料繫結

最近在工作中使用static而引發的思考 先看demo Demo1 .xaml

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
<TextBox Text="{Binding Personx.Namex}" Margin="10"/> <Button Height="20" Click="Button_Click" Margin="10" x:Name ="xamlTocs" Content="xamlTocs"/> <Button Height="20" Click="Button_Click" Margin="10" x:Name ="csToxaml" Content="csToxaml"/> <
/StackPanel> </Grid> </Window>

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.
Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Microsoft.Practices.Prism.ViewModel; namespace WpfApplication4 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { Home Home1 = new Home(); public MainWindow() { InitializeComponent(); this.DataContext = Home1; } private void Button_Click(object sender, RoutedEventArgs e) { if (sender.Equals(this.xamlTocs)) { MessageBox.Show(Home.Personx.Namex); } else { Home.Personx.Namex="Hello"; } } } class Home { private static Person _personx; public static Person Personx { get { return _personx ?? (_personx = new Person { }); } } } class Person : NotificationObject { private string _namex; public string Namex { get { return _namex; } set { _namex = value; RaisePropertyChanged(() => Namex); } } } }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Demo2 .xaml

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <TextBox Text="{Binding  Homex.Personx.Namex}"  Margin="10"/>
            <Button Height="20" Click="Button_Click" Margin="10" x:Name ="xamlTocs" Content="xamlTocs"/>
            <Button Height="20" Click="Button_Click" Margin="10" x:Name ="csToxaml" Content="csToxaml"/>
        </StackPanel>
    </Grid>
</Window>

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        City city  = new City();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = city;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (sender == this.xamlTocs)
            {
                MessageBox.Show(Home.Personx.Namex);
            }
            else
            {
                Home.Personx.Namex="Hello";
            }
        }
    }
    class City
    {
        private Home _homex;
        public  Home Homex
        {
            get
            {
                return _homex ?? (_homex = new Home
                {
                });
            }
        }
         
    }

    class Home
    {
        private static Person _personx;
        public static Person Personx
        {
            get
            {
                return _personx ?? (_personx = new Person
                {
                });
            }
        }

    }

    class Person : NotificationObject
    {
        private string _namex;
        public string Namex
        {
            get { return _namex; }
            set
            {
                _namex = value;
                RaisePropertyChanged(() => Namex);
            }
        }
    }
}

在demo1和demo2中的Xaml檔案中出現了這樣的寫法(Personx是一個靜態屬性!!) 1,Text="{Binding Personx.Namex}" 繫結的物件是this.DataContext = Home1; Personx是屬於Home類中的靜態屬性

2,Text="{Binding Homex.Personx.Namex}" 繫結的物件是this.DataContext = city; HomexCity類中的屬性 PersonxHomex中的靜態屬性

首先

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test _test = new Test();
            _test.
            //我們是無法通過一個物件(在c#中我們還是稱為物件例項的引用吧)來訪問靜態的,因為靜態成員不存在物件例項中,也沒有相應的指標指向。(可能是這樣的設定)
        }
    }

    class Test
    {
        public static int x;
    }
}

但是,在Xaml檔案中 我們好像可以通過這樣的語法實現相應的繫結

Homex.Personx  

Personx是Home類中的靜態成員 但是這樣是可以通過 Homex這個例項引用來訪問靜態變數,而不是通過類。

我YY下哦…這算不算是不是微軟的一個BUG,還是我的理解有不到之處。

畢竟在曾經的劉鐵錳老師的視訊中提到: 微軟中規定:Event事件只能出現在+=和-=的左邊,而實際情況並非一定如此。 當我們使用事件簡化宣告時,我們可以這樣操作 :Event事件!=null 劉老師說:這是微軟在設計事件這個語法糖衣時,造成了語法的前後不一致! (詳細請看劉鐵錳老師的視訊 https://www.bilibili.com/video/av7606481/?p=22)