3.DataTemplate ..._avalonia contentcontrol 赋值">
当前位置:   article > 正文

【Avalonia】Avalonia的学习笔记以及与WPF的不同点

avalonia contentcontrol 赋值

以下均为初学入门写的一些学习记录,不保证理解都对啊。。。

1.axaml中引用命名空间

xmlns:model="using:IDataTemplateSample.Models"

2.Grid支持行列的简化写法

<Grid RowDefinitions="Auto, Auto, *" ColumnDefinitions="Auto, *"/>

3.DataTemplate 根据DataType自动选择对应类型的样式,使用Window.DataTemplates加载多个DataTemplate自动选择显示不同样式,类似WPF的DataTemplateSelector选择器

  1. <Window.DataTemplates>
  2. <!-- Add a DataTemplate for a Student -->
  3. <!-- Mind the order of the Templates. Begin with the most specific first. -->
  4. <DataTemplate DataType="model:Student">
  5. <StackPanel>
  6. <TextBlock FontWeight="Bold" Text="{Binding Grade, StringFormat='I am a student in {0}. grade'}" />
  7. <!-- We re-use the PersonTemplate here by using DynamicResource -->
  8. <ContentControl Content="{Binding}" ContentTemplate="{DynamicResource My.DataTemplates.Person}" />
  9. </StackPanel>
  10. </DataTemplate>
  11. <!-- Add a DataTemplate for a Teacher -->
  12. <DataTemplate DataType="model:Teacher">
  13. <StackPanel>
  14. <TextBlock FontWeight="Bold" Text="{Binding Subject, StringFormat='I am a teacher for: &quot;{0}&quot;'}" />
  15. <!-- We use a UserControl here to display the data -->
  16. <view:PersonView />
  17. </StackPanel>
  18. </DataTemplate>
  19. </Window.DataTemplates>

 4.前面说使用Window.DataTemplates类似WPF的选择器,实则Avalonia也有选择器

先创建一个选择器,选择器的Key和Datatemplate组成ShapesTemplateSelector的数据,根据DataType的类型然后通过选择器的Build创建出对应的数据模版。 

  1. <Window.DataTemplates>
  2. <dataTemplates:ShapesTemplateSelector>
  3. <DataTemplate x:Key="RedCircle" DataType="model:ShapeType">
  4. <Ellipse Width="50"
  5. Height="50"
  6. Fill="Red"
  7. Stroke="DarkRed"
  8. StrokeThickness="2" />
  9. </DataTemplate>
  10. <DataTemplate x:Key="BlueCircle" DataType="model:ShapeType">
  11. <Ellipse Width="50"
  12. Height="50"
  13. Fill="Blue"
  14. Stroke="DarkBlue"
  15. StrokeThickness="2" />
  16. </DataTemplate>
  17. <DataTemplate x:Key="RedSquare" DataType="model:ShapeType">
  18. <Rectangle Width="50"
  19. Height="50"
  20. Fill="Red"
  21. Stroke="DarkRed"
  22. StrokeThickness="2" />
  23. </DataTemplate>
  24. <DataTemplate x:Key="BlueSquare" DataType="model:ShapeType">
  25. <Rectangle Width="50"
  26. Height="50"
  27. Fill="Blue"
  28. Stroke="DarkBlue"
  29. StrokeThickness="2" />
  30. </DataTemplate>
  31. </dataTemplates:ShapesTemplateSelector>
  32. </Window.DataTemplates>
  1. public class ShapesTemplateSelector : IDataTemplate
  2. {
  3. // This Dictionary should store our shapes. We mark this as [Content], so we can directly add elements to it later.
  4. [Content]
  5. public Dictionary<string, IDataTemplate> AvailableTemplates { get; } = new Dictionary<string, IDataTemplate>();
  6. // Build the DataTemplate here
  7. public Control Build(object? param)
  8. {
  9. var key = param?.ToString(); // Our Keys in the dictionary are strings, so we call .ToString() to get the key to look up
  10. if (key is null) // If the key is null, we throw an ArgumentNullException
  11. {
  12. throw new ArgumentNullException(nameof(param));
  13. }
  14. return AvailableTemplates[key].Build(param); // finally we look up the provided key and let the System build the DataTemplate for us
  15. }
  16. // Check if we can accept the provided data
  17. public bool Match(object? data)
  18. {
  19. // Our Keys in the dictionary are strings, so we call .ToString() to get the key to look up
  20. var key = data?.ToString();
  21. return data is ShapeType // the provided data needs to be our enum type
  22. && !string.IsNullOrEmpty(key) // and the key must not be null or empty
  23. && AvailableTemplates.ContainsKey(key); // and the key must be found in our Dictionary
  24. }
  25. }

5.可以创建Styles.axaml文件,里面添加多个样式,使用Design.PreviewWith进行样式预览

  1. <Styles xmlns="https://github.com/avaloniaui"
  2. xmlns:controls="using:RatingControlSample.Controls"
  3. xmlns:converter="using:RatingControlSample.Converter"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  5. <Design.PreviewWith>
  6. <StackPanel Spacing="10">
  7. <controls:RatingControl Value="0" NumberOfStars="5" />
  8. <controls:RatingControl Value="2" NumberOfStars="5" />
  9. <controls:RatingControl Value="6" NumberOfStars="6" />
  10. </StackPanel>
  11. </Design.PreviewWith>

通过selector定义名字,具体名称="控件名.名称",不带名字则表示该类控件全部生效

  1. <Style Selector="TextBlock.txt">
  2. <Setter Property="FontSize" Value="24"/>
  3. <Setter Property="FontWeight" Value="Bold"/>
  4. </Style>

通过classes来使用样式,支持以空格分割的样式类,可以组合多个样式

<TextBlock Classes="txt xxx yyy" Text="TextBlock"/>

也支持代码增加删除样式:

在其他文件中引用样式,添加StyleInclude进行引用,设置Source属性

 6.样式主题

有时候我们可以需要把多个样式组合起来一起使用, 定义一个主题,比如Dark主题,Light主题、Mac主题、Win主题等
下面我们定义俩个文件夹。里面都新增一个Styles文件
0
俩个文件这俩个颜色设置为不同的,便于区分
0
0
然后定义俩个主题文件, 这俩个类里面 include 各文件夹下的所有Style文件,
至于对应的cs文件,需要手动添加,同时继承Styles类。
全部代码如下图所示
0
0
接下来,我们就可以在启动的时候,通过判断不同的平台环境。来加载不同的样式资源
0
运行过程中也可以再次修改
  1. var btnWindow = this.FindControl<Button>("btnWindow");
  2. btnWindow.Click += (s, e) =>
  3. {
  4. this.Styles.Clear();
  5. this.Styles.Add(new WindowsTheme());
  6. };
  7. var btnMacos = this.FindControl<Button>("btnMacos");
  8. btnMac.Click += (s, e) =>
  9. {
  10. this.Styles.Clear();
  11. this.Styles.Add(new MacosTheme());
  12. };

参考:https://www.cnblogs.com/mchuang/p/17326477.html

7.和WPF一样,WPF中MouseUp直接使用没有效果,avalonia中的PointerReleased事件也不能直接触发,需要调用AddHandler

           this.AddHandler(Button.PointerReleasedEvent, btnClose_PointerReleased/*事件调用函数*/, Avalonia.Interactivity.RoutingStrategies.Bubble, true);

 8.在Styles中使用触发器

如下代码功能是鼠标悬浮在类名为rightDirection的按钮的子控件Border上背景变红,此处Border是重写了Button的Template里面加了Border。

  1. <Style Selector="Button.rightDirection>Border:pointerover">
  2. <Setter Property="Background" Value="Red"></Setter>
  3. </Style>

 9.按钮想要高度适应父控件,WPF Grid Row的高度是* 就可以了,但是avalonia好像不行,需要设置VerticalAlignment="Stretch"

    <Button Name="btnRight"  Classes="rightDirection" Margin="0" Grid.Row="0" Grid.Column="2" Grid.RowSpan="2"  VerticalAlignment="Stretch"></Button>

 10、又遇到了一个问题,在后台使用 this.DataContext=new ViewModel();貌似绑定没生效。

  1. this.DataContext = _viewmodel = new MainWindowViewModel();//这样没生效
  2. //下面这样就生效了在Xaml里面写
  3. <Design.DataContext>
  4. <vm:MainWindowViewModel></vm:MainWindowViewModel>
  5. </Design.DataContext>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/743694
推荐阅读
相关标签
  

闽ICP备14008679号