微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin.Forms如何在后面的代码中调用从项目源创建的 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"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。