06 April 2017

Windows Store và Controls: Custom Controls Blink Text trên App Windows Store C#

MainPage.xaml
Windows Store 2017
<Page
    x:Class="CustomControlSample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CustomControlSample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <local:MyCustomControl HorizontalAlignment="Left" Blink="True" Margin="401,258,0,0" VerticalAlignment="Top" Height="188" Width="573"/>

    </Grid>
</Page>
MyCustomControl.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace CustomControlSample
{
    public sealed class MyCustomControl : Control
    {
        public MyCustomControl()
        {
            this.DefaultStyleKey = typeof(MyCustomControl);
        }

        public static readonly DependencyProperty BlinkProperty = DependencyProperty.Register("Blink",
            typeof(bool),
            typeof(MyCustomControl),
            new PropertyMetadata(false, new PropertyChangedCallback(OnBlinkChanged)));



        public bool Blink
        {
            get
            {
                return (bool)GetValue(BlinkProperty);
            }
            set
            {
                SetValue(BlinkProperty, value);
            }
        }

        private DispatcherTimer __timer = null;

        private DispatcherTimer _timer
        {
            get
            {
                if (__timer == null)
                {
                    __timer = new DispatcherTimer();
                    __timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
                    __timer.Tick += __timer_Tick;
                }
                return __timer;
            }

        }

        private void __timer_Tick(object sender, object e)
        {
            DoBlink();
        }

        private void DoBlink()
        {
            this.Opacity = (this.Opacity + 1) % 2;
            OnBlinked();
        }

        public event EventHandler Blinked;
        private void OnBlinked()
        {
            EventHandler eh = Blinked;
            if (eh != null)
            {
                eh(this, new EventArgs());
            }
        }

        private static void OnBlinkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = d as MyCustomControl;
            if (instance != null)
            {
                if (instance._timer.IsEnabled != instance.Blink)
                {
                    if (instance.Blink)
                    {
                        instance._timer.Start();
                    }
                    else
                    {
                        instance._timer.Stop();
                    }
                }
            }
        }
    }
}
Generic.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:CustomControlSample">

    <Style TargetType="local:MyCustomControl" >
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
      
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:MyCustomControl">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBlock Text="Hello World" FontFamily="Arial" FontSize="36"></TextBlock>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang