wpf中的响应式设计- c#应用程序



我希望我的英语足够好,可以让你明白我的意思。

我有一些问题。我想用c#在Visual Studio中为我的wpf应用程序做一个响应式设计。

起初我想做一个设计框架,像Bootrap或UIKit在CSS中,但这只是一个实验为我自己。

我知道存在ResourceDirectory for "static"设计,我知道我通常不能改变这些文件中的设计,但我不知道,如果存在一个解决方案的动态变量和相同的。作为备选方案,我认为我可以在应用程序资源中进行设计,但我还没有实际测试,这个解决方案有多好。我如何在App.xaml.cs或显式ViewModel中更改变量。它是否等于所有其他Windows与ViewModel?

我找到了一个响应式设计的"例子"。在Youtube上,但我不知道这些布局是如何工作的。有人能解释一下吗?我根本不想使用这个解决方案,因为我想使用类似框架的设计。WPF响应式设计

设计工艺本身存在一些问题:

  • 触发器可以单独存在,还是可以单独存在,例如,在

  • 如何从Child-Textblock中的按钮获取内容→我如何从按钮的内容,如果我有一个Viewbox在这个按钮→我可以把它设置为模板"动态"内容

  • 我如何创建一个模板,谁有多个模板和/或样式在它。(依赖属性),对于样式,这里存在一个解决方案。样式类

  • 当我使用一个转换器,我可以直接改变变量,而不是实现数以千计的触发器常量,我希望Setter值是可变的转换器。

  • 一个额外的选择,我想在CompilerTime中为配置设置ResourceDirectory,但这在c#中是可能的吗?我的意思是叫元编程,但我不知道。例如:有人不想使用我的变量,而是使用其他配置,所以他将我的变量设置为新的,以使用他的配置。因为ResourceDirectory是通过编译一次构建的,所以我认为必须在编译时进行配置。

我希望你能帮我解决所有这些问题。

所有代码在ResourceDirectory中,可选的在App.xaml中的ApplicationResource

带有Textblock和Viewbox的按钮模板

<ControlTemplate x:Key="Example">
<Button>
<Button.ContentTemplate>
<DataTemplate>
<Viewbox>
<TextBlock Text="Content From Button"> Or Get Here Content From Button</TextBlock>
<!--// Like <TextBlock Text="{this.button.content}"/> //-->
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</ControlTemplate>

In Style/Style。触发在资源目录或ApplicationResource

中设置新值
<Style x:Key="My_BTN" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=Window},
Converter={x:Static local:Converter.Instance}, ConverterParameter=1400}" Value="true">
<Setter Property="Background" Value="{x:Static local:Example.SomeValue}"/>
</DataTrigger>
</Style.Triggers>
</Style>

所以,我已经解决了3个问题。

  1. 我已经发现了,我如何在触发器中使用按钮属性,所以我认为我不需要单独的触发器。我必须像点二一样使用相对源。转换器:
<Style x:Key="BTN_Error" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}, Converter={x:Static local:IsGreaterThen.Instance}, ConverterParameter=20}" Value="true">
<Setter Property="Height" Value="200"></Setter>
<Setter Property="Background" Value="Red"></Setter>
<Setter Property="Content" Value="{Binding Path=Content, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Viewbox>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Content}"></TextBlock>
</Viewbox>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
<Setter Property="Height" Value="50"></Setter>
</Style>
  1. 如果我只使用数据模板,我可以使用来自按钮的内容。这样的:
<DataTemplate x:Key="flex_font">
<Viewbox>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Content}"></TextBlock>
</Viewbox>
</DataTemplate>
  1. 我想我不再需要Multitemplates了,我可以用setter设置模板的样式。如果模板是datatemplate,它们不会在样式集成之前或之后重写样式。
<Style x:Key="BTN_Warning" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="BorderBrush" Value="Gold"/>
<Setter Property="ContentTemplate" Value="{StaticResource flex_font}" />
</Style>

所以最重要的问题仍然悬而未决。如果我使用转换器,我可以像我的第一个帖子一样改变值吗?最后,我想,如果我知道我如何使用app . example .cs来改变变量,在运行时,有足够的解决方案,使我的响应式设计框架。

我想我应该像这样改变App.xaml.cs中的变量。

Class MyWindow: Window {
// Use Constructor just as an Example
public MyWindow(){
// I Don't know how to call the App-Class correct
// Blue for Change Color as an Example
App.Variable = "Blue";
}
}

最新更新