0507 日常记录需要了解的

IValueConverter

    public class Conver : IValueConverter
    {
        //当值从绑定源传播给绑定目标时,调用方法Convert
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)

            return date.ToString("yyyy-MM-dd");
        }
        //当值从绑定目标传播给绑定源时,调用此方法ConvertBack
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string str = value as string;

            return DependencyProperty.UnsetValue;
        }
    }

 

<Window.Resources>
    <local:Conver x:Key="ConverR"/>
</Window.Resources>
<TextBox x:Name="textBox" Text="{Binding ElementName=zzzz,Path=xxxxx,Converter={StaticResource ConverR}}" />

https://www.cnblogs.com/tianma3798/p/5927470.html

ObservableCollection

ObservableCollection<T> 类    表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

ObservableCollection<Students> infos = new ObservableCollection<Students>();

INotifyPropertyChanged会向客户端发出某一属性值已更改的通知。当元素属性值改变时,会通知后台model

前台代码不变,我们让后台Students  Model实现INotifyPropertyChanged接口。

public class Students : INotifyPropertyChanged
        {
            string _name;
            public int Id { get; set; }
            public string Name
            {
                get { return _name; }
                set { _name = value; OnPropertyChanged("Name"); }
            }
            public int Age { get; set; }
            protected internal virtual void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            public event PropertyChangedEventHandler PropertyChanged;
        }

 

ItemsSource="{Binding StudentList, Mode=TwoWay}"

$  string y = $"{s} world"; 等同于使用Format方法

string id = "123"; string tid = $"ds{id}";  执行完tid等于ds123

忽略转义字符

string fileName = @"D:\文本文件\text.txt";

让字符串跨行

string strSQL = @"SELECT * FROM HumanResources.Employee AS e INNER JOIN Person.Contact AS c ON e.ContactID = c.ContactID ORDER BY c.LastName";

SQL语句占位

string retid=1;

string sql=$“delete from test where id={retid}”

string sql1=“select * from test id=@pid”

parameters.add(new parameter(“@pid”,int,pid));

Dictionary<string,string> KeyValue=new Dictionary<string,string>();

KeyValue.add(“id”,“@pid”);

parameters.add(new parameter(“@pid”,int,pid));

触发器

EventTrigger改变其他控件的值

使用EventTrigger后触发StoryBoard,通过StoryBoard改变其他控件的值。

Trigger:根据某一个依赖属性的变化,用Setter更改某些样式。

MultiTrigger:只有多个依赖属性的的变化同时都满足时,触发器才会生效。

DataTrigger:可以完成Trigger的所有功能,也可以完成对非依赖属性的监听。DataTrigger一共引入了三个参数:Binding,Value和Setters。当需要设置数据触发器侦听的数据源时,通过绑定对Binding属性赋值的方式来完成。

MultiDataTrigger:只有多个属性的的变化同时都满足时,触发器才会生效。

EventTrigger:事件被触发时完成一个动画。

https://www.cnblogs.com/greenteaone/archive/2012/12/17/2811082.html

RoutedEvent  https://blog.csdn.net/litao2/article/details/54947009

BeginStoryboard:要在EventTrigger中触发调用,RoutedEvent是响应的事件

        <EventTrigger RoutedEvent="Window.Loaded" SourceName="win">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation From="1" To="100" RepeatBehavior="Forever" Duration="0:0:5" 
Storyboard.TargetName="Line1" Storyboard.TargetProperty="StrokeDashOffset"/>
                        <DoubleAnimation From="1" To="100" RepeatBehavior="Forever" Duration="0:0:5" 
Storyboard.TargetName="Line2" Storyboard.TargetProperty="StrokeDashOffset"/>
                        <DoubleAnimation From="1" To="100" RepeatBehavior="Forever" Duration="0:0:5" 
Storyboard.TargetName="Line3" Storyboard.TargetProperty="StrokeDashOffset"/>
                        <DoubleAnimation From="100" To="1" RepeatBehavior="Forever" Duration="0:0:5" 
Storyboard.TargetName="Line4" Storyboard.TargetProperty="StrokeDashOffset"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>

DependencyProperty

// 注册一个新的DP:NameProperty
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(DependencyObject), string.Empty);
ResourceDictionary 

1、创建资源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
                    xmlns:system="clr-namespace:System;assembly=mscorlib"
                    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
                    xmlns:controlsPrimitives="clr-namespace:Microsoft.Phone.Controls.Primitives;assembly=Microsoft.Phone.Controls">

    <!--如果想预览界面效果,把一下代码加到页面里。不加的话在运行时匹配.-->
    <!--<phone:PhoneApplicationPage.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Design/Design.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </phone:PhoneApplicationPage.Resources>-->

    <!--(主)字体颜色-->
    <SolidColorBrush x:Key="Host_Color" Color="Black"/>

    <!--(所有页面)正在加载提示-->
    <Style TargetType="TextBlock" x:Key="LoadingTextBlock">
        <Setter Property="FontSize" Value="25"/>
        <Setter Property="Text" Value="正在加载"/>
        <Setter Property="Foreground" Value="#FF476FBF"/>
    </Style>
    <Style TargetType="ProgressBar" x:Key="LoadingProgressBar">
        <Setter Property="IsIndeterminate" Value="True"/>
        <Setter Property="Width" Value="320"/>
        <Setter Property="Margin" Value="-20,0,0,-15"/>
        <Setter Property="Foreground" Value="#FF476FBF"/>
    </Style>

    <!--定义样式资源-->
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="{StaticResource Host_Color}"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
    </Style>

</ResourceDictionary>

Style的x:Key属性是资源字典里面的资源的唯一的标示符,也是作为在其他页面调用的一个唯一的Key来进行调用。

2、调用资源资源中的资源

<Window:BaseUserControl.Resources>
     <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Design.xaml"/>
          </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
 </Window:BaseUserControl.Resources>

 

注意:Source的路径是指当前使用Design.xaml文件的页面,相对Design.xaml的位置;例如
----文件夹View

      ----文件Page.xaml

----Design.xaml

如果文件Page.xaml要使用Design.xaml,Source="../Design.xaml"

ResourceDictionary.MergedDictionaries   获取 ResourceDictionary 字典的集合,这些字典构成了合并字典中的各种资源字典。 

3、使用字典资源中的资源

<TextBlock Text="Some Text" Style="{StaticResource LoadingTextBlock}"/>

StaticResource 通过替换已定义资源的值来为 XAML 属性提供值。

DynamicResource 通过将值推迟为对资源的运行时引用来为XAML 属性提供值。动态资源引用强制在每次访问此类资源时都重新进行查找。

静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了;动态资源(DynamicResource)使用指的是在程序运行过程中然会去访问资源。

加入事件代码(i:Interaction.Triggers)

  <i:Interaction.Triggers>
               <i:EventTrigger EventName="OnToolTipShow">
                <cmd:EventCommand Command="{Binding OnToolTipShow}"/>
 </i:EventTrigger>

<ItemsControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.MenuRegion }"/>

prism:InteractionRequestTrigger

在view层次,prism提供了InteractionRequestTrigger,这个类能自动连接到IInteractionRequest接口的Raised 事件

https://www.cnblogs.com/qianzi067/p/5804889.html?utm_source=itdadao&utm_medium=referral

 

public event EventHandler<InteractionRequestedEventArgs> Raised;

this.confirmCancelInteractionRequest.Raise(
new Confirmation("Are you sure you wish to cancel?"),
confirmation =>
{
if (confirmation.Confirmed)
{
this.NavigateToQuestionnaireList();
}
});
 

<i:Interaction.Triggers>
<prism:InteractionRequestTrigger
SourceObject="{Binding ConfirmCancelInteractionRequest}">
<prism:PopupChildWindowAction
ContentTemplate="{StaticResource ConfirmWindowTemplate}"/>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
<UserControl.Resources>
<DataTemplate x:Key="ConfirmWindowTemplate">
<Grid MinWidth="250" MinHeight="100">
<TextBlock TextWrapping="Wrap" Grid.Row="0" Text="{Binding}"/>
</Grid>
</DataTemplate>
</UserControl.Resources>

InteractionRequestTrigger

通过Prism中提供的InteractionRequestTrigger事件触发器,实现点击按钮或者用户的某种操作弹出对话框的效果

创建ChildWindow的基类

新建类:ChildWindowActionBase 并从TriggerAction<T>派生

https://www.cnblogs.com/tianciliangen/p/4961045.html

public class ChildWindowActionBase : TriggerAction<FrameworkElement>

WPF委托命令DelegateCommand的传参方式

<!-- 无参方式 -->
<Button Content="Test Command" Command="{Binding TestCommand}" />

<!-- 将自己作为参数 -->
<Button Content="Test Command2" Command="{Binding TestCommand2}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" >

<!-- 将父元素作为参数 -->
<Button Content="Test Command3" Command="{Binding TestCommand3}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}}" >

1.DelegateCommand

2.RelayCommand

3.AttachbehaviorCommand

/// <summary>
    /// Delegatecommand,这种WPF.SL都可以用,VIEW里面直接使用INTERACTION的trigger激发。比较靠谱,适合不同的UIElement控件
    /// </summary>
    public class DelegateCommand : ICommand
    {
        Func<object, bool> canExecute;
        Action<object> executeAction;
        bool canExecuteCache;

        public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
        {
            this.executeAction = executeAction;
            this.canExecute = canExecute;
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            bool temp = canExecute(parameter);

            if (canExecuteCache != temp)
            {
                canExecuteCache = temp;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, new EventArgs());
                }
            }

            return canExecuteCache;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            executeAction(parameter);
        }

        #endregion
    }

https://www.cnblogs.com/matoo/archive/2012/04/14/2447159.html

 

Prism定义了一个EventTrigger——InteractionRequestTrigger,可以自动和IInteractionRequest接口的Raised事件进行连接。这减少了XAML的代码量,同时减少了错误输入事件名称的可能。 
事件触发以后,InteractionRequestTrigger会调用指定的动作。Prism为WPF提供了PopupWindowAction类,这个类可以显示一个弹出窗口。当窗口显示时,它的数据上下文设置为交互请求的上下文参数。使用PopupWindowAction的WindowContent属性可以指定弹出窗口的视图。弹出窗口的标题被绑定到上下文对象的Title属性。

 

Window标签中的xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity",这里相当于后台的using System;之类的加载程序集的功能

 

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
public void SubmitClicked()
{
   MessageBox.Show("Button was clicked");
}
<Button Content="Submit Method"  Width="180">
     <i:Interaction.Triggers>
         <i:EventTrigger EventName="Click">
               <ei:CallMethodAction TargetObject="{Binding}" MethodName="SubmitClicked"/>
         </i:EventTrigger>
      </i:Interaction.Triggers>
</Button>

<i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i:InvokeCommandAction Command="{Binding Command1}" CommandParameter="10" />
                </i:EventTrigger>
                <i:EventTrigger EventName="MouseMove">
                 <i:InvokeCommandAction Command="{Binding Command2}" CommandParameter="{Binding ElementName=btn}" />
                </i:EventTrigger>
 </i:Interaction.Triggers>

public ICommand Command1 {
            get {
                return new DelegateCommand<string>((str) => {
                    MessageBox.Show("Command1 with parameter:"+str);
                });
            }
        }
 
public ICommand Command2 {
            get {
                return new DelegateCommand<Button>((button) => {
                    Point p = Mouse.GetPosition(button);
                    button.Content = string.Format("{0},{1}", p.X, p.Y);
                });
            }
        }

Notification通知式

Prism通过InteractionRequest 来实现弹窗交互,它是一个泛型接口,不同的类型对应不同类型的弹窗方式。
在使用InteractionRequest的时候需要在,xaml中需要注册一个Trigger:

    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" />
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>

Interaction

这里用到了Interaction,他是i命名空间里的东西,那么i是什么呢?
interactivity这个是微软内置的类库,他提供了一组用户交互的类,比如我们这里用到的EventTrigger可以用来执行事件触发的操作。
在使用的时候,先引入xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
或者xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity",然后在xaml中使用他:

<i:Interaction.Triggers>
    <i:EventTrigger>

    </i:EventTrigger>
</i:Interaction.Triggers>

而 prism:PopupWindowAction 的 IsModal=True意味着弹框不被关闭的时候,父窗体无法使用。我刚搜索了一下,这个词的翻译竟然“模态”。

 

ref 和out

https://blog.csdn.net/m0_37679113/article/details/83045813

一个派生的视图模型类可以在属性的设值处调用SetProperty方法。该方法会检查字段是否与将要设值的值相同,如果不同,字段会被更新,PropertyChanged事件会触发。 

 重要

https://www.cnblogs.com/qianzi067/p/5804880.html?utm_source=itdadao&utm_medium=referral

private int itemNo;

public int ItemNo

{

      get{ return itemNo;}

      set{SetProperty(ref itemNo,value); }

 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值