【问题标题】:Xamarin.Forms. How to call methods of xaml element created from item source in code behind?Xamarin.Forms。如何在后面的代码中调用从项目源创建的 xaml 元素的方法?
【发布时间】:2021-07-29 12:15:37
【问题描述】:

在这种情况下如何调用 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();
}

【问题讨论】:

  • 请不要将代码或错误作为图像发布。而且您不能按名称引用模板中的项目。这已在数百个问题上反复讨论过。
  • @Jason 好的,感谢您的 cmets,请问在这种情况下是否有任何方法可以调用给定对象的方法?
  • 通常您不应直接与模板化 UI 对象进行交互。这实际上取决于具体的用例。因为 ListView 可以包含许多项,所以一个 UI 元素可能有许多副本,这就是为什么您不能通过名称来解决它们。
  • @Jason 我正在使用CSharpMath 库来显示带有公式的文本,更新发布后,内容消失了,我写信给github上的开发人员,他们回答了我会使用 InvalidateSurface 方法,但事实证明在这种情况下我不能这样做?
  • 如果你需要这样做,我会尝试为在其构造函数中调用 InvalidateSurface 的控件编写一个包装器

标签: xaml xamarin xamarin.forms data-binding


【解决方案1】:

为了解决这个问题,我使用了一个包装类:

public class MyTextView : TextView
{
    public MyTextView()
    {
        InvalidateSurface();
    }
}

并在 xaml 文件中使用了这个类:

<views:MyTextView x:Name="textView" LaTeX="{Binding Formula}" Margin="15, 5"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多