如何以正确的方式实施 UWP

How to implement UWP the right way

我 运行 经常遇到许多导致重构我的代码的问题...

这就是为什么我想寻求一些建议。

我运行遇到的问题是:

1) 向 XAML

提供数据

提供简单的数据来控制价值,而不是使用价值转换器。例如,我有一个颜色字符串,如“#FF234243”,它存储在 class 中。字符串的值由 Web 应用程序提供,因此我只能在 运行 时间指定它。

2) UI 每个分辨率

在我刚开始学习的时候,有人告诉我你可以为每个可能的分辨率创建一个 UI,这是愚蠢的。 所以我写了一个 ValueConverter,我绑定在一个元素上,作为 ConverterParameter,我给出了一个像“300”这样的值,它会针对每个可能的分辨率进行计算……但这会导致这样的代码……

<TextBlock
Height={Binding Converter={StaticResource SizeValue}, ConverterParameter='300'}
/>

3) DependencyProperties 与 NotifyProperties(实现 INotifyPropertyChanged 的​​属性)与 Properties

我写了一个控件,它接受一个值列表并将它们转换成 按钮 ,它们可以在 UI 中单击。所以我这样做了 我创建了一个变量,我为这个特定的 Control 设置为 DataContext 并使用 DataContextChanged 验证我的数据 但我的同事提到,出于这个原因 DependencyProperties 在哪里引入。所以我创建了一个 DependecyProperty ,当 属性 获得一个值时,它获取项目列表 BUT 我必须渲染按钮.. . 所以我必须做类似

public List<string> Buttons
        {
            get { return (List<string>)GetValue(ButtonsProperty); }
            set
            {
                SetValue(ButtonsProperty, value);
                RenderButtons();
            }
        }
        // Using a DependencyProperty as the backing store for Buttons.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ButtonsProperty =
            DependencyProperty.Register("Buttons", typeof(List<string>), typeof(MainPage), new PropertyMetadata(""));

        private void RenderButtons()
        {
            ButtonBar.Children.Clear();
            ButtonBar.ColumnDefinitions.Clear();

            if(Buttons != null)
            {
                int added = 0;
                foreach (var item in Buttons)
                {
                    var cd = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) };
                    var btn = new Button() { Content = item };
                    ButtonBar.ColumnDefinitions.Add(cd);
                    ButtonBar.Children.Add(btn);
                    Grid.SetColumn(btn, added);
                }
            }
        }

并且必须像这样使用它:

<Controls:MyControl
    x:Name="ButtonBar" Button="{Binding MyButtons}">
</Controls:MyControl>

由于这些主题很多,我可以将它们分开,但我认为这对初学者来说是一个很常见的主题,我还没有找到任何解释或其他任何内容

1.向 XAML

提供数据

有两种选择:在 ViewModel 中准备数据或使用转换器。 在我看来,使用转换器更好,因为您可以像示例中提到的那样拥有带颜色的跨平台 viewModel,并且转换器将创建依赖于平台的颜色。我们在图像方面遇到了类似的问题。在 android 上,它应转换为位图 class,而在 UWP 上,它应转换为 BitmapImage class。在 viewModel 中我们有 byte[]。

2。 UI 每个分辨率

您不需要使用转换器,因为高度是以有效像素为单位指定的,它将自动为您适合所有需要的分辨率。可以在以下 link:

找到更多信息

https://docs.microsoft.com/en-us/windows/uwp/design/layout/layouts-with-xaml

有两种处理文本块大小的选项:

a) 使用预定义的文本块样式,不要发明轮子(推荐选项):

https://docs.microsoft.com/en-us/windows/uwp/design/style/typography#type-ramp

b) 以像素为单位指定字体大小。它们不是像素,而是有效像素。它们将在不同的设备上自动缩放:

https://docs.microsoft.com/en-us/windows/uwp/design/style/typography#size-and-scaling

此外,使用 adaptive layout 为不同的屏幕尺寸设置不同的布局。

3) DependencyProperties 与 NotifyProperties(实现 INotifyPropertyChanged 的​​属性)与 Properties

根据您的代码,您可以尝试使用 ListView or ItemsControl 并定义自定义项目模板。

DependencyProperties 在 DependencyObject 中创建,可在 xaml 中访问。所有控件都继承自 DependencyObjects。通常当您想在 xaml 中设置它们时创建它们。它们不直接存储在对象中,而是存储在全局字典中并在运行时解析。

DependencyProperties 是很久以前创建的,您可以找到很多 links 来详细解释它们:

http://www.wpftutorial.net/dependencyproperties.html

https://techpunch.wordpress.com/2008/09/25/wpf-wf-what-is-a-dependency-property/

When should I use dependency properties in WPF?

What is a dependency property? What is its use?

What is a dependency property?

INotifyPropertyChanged INPC 是 MVVM 的核心部分。您将视图绑定到实现 INPC 的 viewModel,当您更改 属性 控件的值时会收到通知并重新读取新值。

下载以下高分辨率视频,其中详细解释了 MVVM(作者:Laurent Bugnion): https://channel9.msdn.com/Events/MIX/MIX11/OPN03

MVVM: Tutorial from start to finish?

正常属性用于模型 classes 或不需要通知 UI 有关更改的情况。