public Control? Build(object? param) { if (param != null && param is ViewModelBase) { var viewModelType = param.GetType(); if (_viewModelViewMappings.TryGetValue(viewModelType, outvar viewType)) { return (Control)Activator.CreateInstance(viewType)!; // 这里使用了反射 } returnnew TextBlock { Text = "Not Found: " + viewModelType.FullName }; } returnnull; }
修改后使用工厂方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public Control? Build(object? param) { if (param != null && param is ViewModelBase) { var viewModelType = param.GetType(); if (_viewModelViewMappings.TryGetValue(viewModelType, outvar viewFactory)) { return viewFactory(); // 使用了工厂方法 } returnnew TextBlock { Text = "Not Found: " + viewModelType.FullName }; } returnnull; }
这样就可以消除反射了
问题2: 关闭特定库的剪裁
发布时我注意到很多这样的剪裁警告
1 2 3 4 5
2>Assembly 'Serilog' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>Assembly 'ReactiveUI' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>Assembly 'SukiUI' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>Assembly 'Avalonia.Controls.DataGrid' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries 2>Assembly 'Avalonia.Controls.DataGrid' produced AOT analysis warnings.
其中<TextBlock Text="{Binding Name}"/>和Text="{Binding Id}"/>产生了报错AVLN2000 Unable to resolve property or method of name 'Id' on type 'XamlX.TypeSystem.XamlPseudoType'.,查看类型ExploreViewModel,定义大概如下
1 2 3 4 5 6 7 8 9
publicclassExploreViewModel : ViewModelBase { public A[] B { get; set; } = new A[] { ... }; }
发现启动后报错 If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.,后来发现可能是没有设定全局组件,文档中有提到:
Components that are used in Markdown has to be marked as global in your Nuxt app if you don’t use the components/content/ directory, visit Nuxt 3 docs to learn more about it. 如果在Nuxt应用中使用的组件未放置在components/content/目录下,则需将其标记为全局组件。
我学习 Rust 已经有一小段时间了,Rustling 这个项目我认为是新手学完Rust基本语法,或者在看完 The Book (中文版) 后,一个很适合的练习题组,这个项目提供了75道 Rust 语言的小题目,提供了方方面面的考验,对于绝大多数题目也都有足够的提示,但是鉴于我没有找到使用中文介绍这些题目的文章,便打算自己开一篇介绍和讲解,本人有的实现可能不够好,或者讲解有谬误,希望各位斧正。