zoukankan      html  css  js  c++  java
  • x名称空间

    XAML代码的WPF程序都需要通过语句:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",x就是用来映射xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",它包含的类均与解析XAML语言相关,所以亦称为"XAML名称空间"

    x名称空间中包含的工具:

     1.x:Name

     x:Name的作用:

    1)告诉XAML编译器,当一个标签带有x:Name时除了为这个标签生成对应实例外还要为这个实例声明一个引用变量,变量名就是x:Name的值

    2)将XAML标签所对应对象的Name属性(如果有)也设为x:Name的值,并把这个值注册到UI树上,以方便查找

    2.x:FieldModifier

    默认情况下,字段的访问级别是internal。x:FieldModifier的作用就是在XAML里改变引用变量的访问级别,例如:

    <TextBox x:Name="textBox1" x:FieldModifier="public" HorizontalAlignment="Left" Height="23" Margin="64,26,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox2" x:FieldModifier="public" HorizontalAlignment="Left" Height="23" Margin="64,74,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="23" Margin="64,124,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> 

     前两个的访问级别改为public,最后一个的访问级别为internal

    3.x:Key

    对应于"Key-Value" 的检索方式。在XAML文件中,可以把多次使用的资源提取出来,放到资源字典(Resource Dictionary)里,需要使用这个资源的时候,就把Key检索出来。x:Key就是用于为资源贴上用于检索的索引

    例子:

    Foreground:

    <Window x:Class="xKey.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow" Height="130" Width="200">
        <!--要使用String,所以要添加:xmlns:sys="clr-namespace:System;assembly=mscorlib"(直接复制黏贴过来就好)-->
        <Window.Resources>
            <sys:String x:Key="myString">Hello WPF Resources!</sys:String>
        </Window.Resources>
        <Grid>
            <StackPanel Background="Gray">
                <TextBox Text="{StaticResource ResourceKey=myString}" Margin="5"></TextBox><!--使用x:Key中的资源-->
                <TextBox x:Name="textBox1" Margin="5"></TextBox>
                <Button Content="Show" Click="Button_Click" Margin="5"></Button>
            </StackPanel>
        </Grid>
    </Window>

     Background:

    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;
    
    namespace xKey
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                string str = this.FindResource("myString") as String;//调用资源
                this.textBox1.Text = str;
            }
        }
    }

    4.x:Share

     在使用x:Key时,一旦把某些对象当做资源放进资源字典后就可以把它们检索出来重复使用。但问题是,它们是不是都是同一个对象呢?这就看给x:Share赋了什么值了。x:Share一定要与与x:Key配合使用,如果x:Share的值为true,则每次检索这个对象时,都是同一个对象,否则,如果为false,则得到的是这个对象的一个新副本。默认情况下,得到的都是同一个对象,即x:Share的值为true

  • 相关阅读:
    摇骰子游戏
    那些年,产品提出的无理需求
    cookie中数据无法读取,HttpOnly属性
    vue 使用字典值及其翻译
    微信小程序picker组件两列关联使用方式
    fatal: unable to access 'https://github.com/xxxxxgit/': OpenSSL SSL_connect: Connection was reset in connection to github.com:443
    Oracle数据库以date类型保存日期时 nvalid request: Out of range property; SalesOrder.date'
    Event Handler Content must support at least one class.
    Minimum length not met: value 0 < 1 minimum length
    Oracle中使用hash_hmac() 函数报错问题/以及Oracle遇到Oauth1.0授权和oauth_signature生成规则
  • 原文地址:https://www.cnblogs.com/KeenLeung/p/3520582.html
Copyright © 2011-2022 走看看