xamarin.forms - 是否可以让视图对象访问视图模型 Xamarin 表单?
问题描述
在我的应用程序中,所有视图模型都具有基本视图模型类,我想向所有具有视图模型的视图添加一些通用视图。有什么方法可以在模型中识别其视图的对象是什么,以便我可以将公共子视图添加到其中
解决方案
你可以定义一个ControlTemplate
with ResourceDictionary
,然后把它App.xaml
作为一个StaticResource
.
例如,常见的视图是Label
:
创建一个commonLabel.xaml
:
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
>
<ControlTemplate x:Key="testcontrol">
<Label Text="this is a common label"/>
</ControlTemplate>
</ResourceDictionary>
然后在你的App.xaml
:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourProject.App">
<Application.Resources>
<ResourceDictionary Source="commonLabel.xaml"/>
</Application.Resources>
</Application>
那么你可以在你的 pages.xaml 中使用:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
NavigationPage.HasNavigationBar="false"
ControlTemplate="{StaticResource BaseTemplate}"
x:Class="YourPage">
<ContentView ControlTemplate="{StaticResource testcontrol}"/>
</ContentPage>
推荐阅读
- nginx - Nginx 根据 cookie 值返回静态文件或代理请求
- typescript - 永久禁用 vuetify v-data-table prev-icon
- python-3.x - 我们如何使用具有范围条件约束的纸浆 python 求解线性方程
- python - How to create a new button by pressing a button in popup (python kivy)
- mapbox - 有没有办法让一个图标一路走来?
- javascript - 悬停在标签上时基于鼠标的视差中断
- javascript - 根据表单值角度禁用表格中的复选框
- python - 在 matplotlib imshow() 中,如何更改现有图上的颜色轴范围?
- javascript - 我可以设置一个 switch 语句来记住如何处理 JavaScript 中的输出和输入吗?
- java - java swing如何解决打印值的问题