28 March 2017

Windows Store và Page: ResourceDictionary trên Windows Store Apps

App.xaml
Windows Store 2017
<Application
    x:Class="ResourceSample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ResourceSample">
    <Application.Resources>
        <ResourceDictionary Source="MyDictionary.xaml"/>
    </Application.Resources>
</Application>
MainPage.xaml
Windows Store 2017
<TextBlock x:Name="textBlock" Foreground="{StaticResource scbColor}" HorizontalAlignment="Left" Margin="490,317,0,0" TextWrapping="Wrap" Text="Hello C1509L" VerticalAlignment="Top" FontSize="36"/>
<TextBlock x:Name="tbHelloWorld" Foreground="{Binding Brush1}" HorizontalAlignment="Left" Margin="493,483,0,0" TextWrapping="Wrap" Text="Hello World!" VerticalAlignment="Top" FontSize="36"/>
MainPage.xaml.cs
Windows Store 2017
public MainPage()
{
    this.InitializeComponent();

    MyColor color = new MyColor();
    color.Brush1 = new SolidColorBrush(Colors.Red);
    tbHelloWorld.DataContext = color;

}
MyDictionary.xaml
Windows Store 2017
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ResourceSample">
    <SolidColorBrush x:Key="scbColor" Color="Green"/>
</ResourceDictionary>
MyDictionary.xaml
Windows Store 2017
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;

namespace ResourceSample
{
    public class MyColor : INotifyPropertyChanged
    {
        //public event PropertyChangedEventHandler PropertyChanged;

        private SolidColorBrush _Brush1;
        public SolidColorBrush Brush1
        {
            get
            {
                return _Brush1;

            }
            set
            {
                _Brush1 = value;
                NotifyPropertyChanged("Brush1");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

        }
    }
}
Run App test

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang