xaml - Xamarin.Forms。如何在后面的代码中调用从项目源创建的 xaml 元素的方法?
问题描述
在这种情况下如何调用 InvalidateSurface 方法?此方法不引用可绑定对象,而是引用 xaml 元素本身。我是应用程序开发的新手,我真的需要弄清楚这一点。
我的 xaml 文件的一部分
<StackLayout>
<ListView
CachingStrategy="RecycleElement"
HasUnevenRows="True"
ItemsSource="{Binding TheoryContentList}"
SelectionMode="None"
SeparatorVisibility="None"
x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:Wrapper">
<ViewCell>
<StackLayout>
<ScrollView>
<math:TextView x:Name="textView" LaTeX="{Binding Formula}" FontSize="{Binding Source={x:Reference TheoryPage1},Path=BindingContext.FSize}" HeightRequest="{Binding HeightReq}"/>
</ScrollView>
<Label LineBreakMode="WordWrap" HeightRequest="{Binding LabelHeightReq}" HorizontalTextAlignment="End" Margin="15, 0">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding BoldText}" FontAttributes="Bold"/>
<Span Text="{Binding Text1}" FontSize="Small" FontAttributes="Italic"/>
<Span x:Name="{Binding }" Text="{Binding Link.Text}" FontSize="Small" TextDecorations="Underline" FontAttributes="Italic" TextColor="Blue">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference TheoryPage1},Path=BindingContext.LinkTappedCommand}" CommandParameter="{Binding Link}"/>
</Span.GestureRecognizers>
</Span>
<Span Text="{Binding Text2}"/>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label x:Name="linkLabel" FontSize="Small" TextColor="Gray" Padding="0" Margin="15,0" HorizontalTextAlignment="Start"/>
</StackLayout>
我的代码在后面
public TheoryPage()
{
InitializeComponent();
BindingContext = _viewModel = new TheoryViewModel();
textView.InvaludateSurface();
}
解决方案
为了解决这个问题,我使用了一个包装类:
public class MyTextView : TextView
{
public MyTextView()
{
InvalidateSurface();
}
}
并在 xaml 文件中使用了这个类:
<views:MyTextView x:Name="textView" LaTeX="{Binding Formula}" Margin="15, 5"/>
推荐阅读
- php - 如何在 Symfony 3.4 中找到当前的路由模式?
- javascript - 无法将 ws 响应分配给 $scope 变量
- java - 以只有一个订阅者会使用它的方式将 java 9 Flow 上的数据发布给订阅者
- tensorflow - 使用 LSTM logits 进行值预测
- ios - 使用您的代码发送短信无限加载屏幕帐户套件IOS
- css - 如何设置 Angular 材质选项卡的 mat-ink-bar 取选项卡标题文本宽度的宽度
- javascript - 如何将 updateend 事件侦听器添加到两个对象?
- javascript - NODE.JS如何返回使用cheerio收集的数据(不返回任何内容)
- node.js - 如何从未标记的嵌套 XML 文件中提取数据?
- emacs - 如何在 Emacs 中设置截断行的宽度