Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重用XML布局_C#_Xml_Xamarin_Xamarin.forms - Fatal编程技术网

C# 如何重用XML布局

C# 如何重用XML布局,c#,xml,xamarin,xamarin.forms,C#,Xml,Xamarin,Xamarin.forms,在我正在制作的应用程序中,我使用下面的XML显示帖子。问题是我需要它能够很容易地被重用,现在显示帖子的每个类都需要XML,所以每当我更改一个值时,我必须遍历所有类来更改每个页面的值 所以我的主要问题是,是否有可能将FormatPosts方法移动到一个名为LayoutUtils的C类中,因此在每个页面的构造函数上,我只使用LayoutUtils.FormatPosts,它返回所有解析的内容或其他内容,并将XML放在一个类中,因此如果我更改它,它会到处更新它 我使用的XML代码是 <

在我正在制作的应用程序中,我使用下面的XML显示帖子。问题是我需要它能够很容易地被重用,现在显示帖子的每个类都需要XML,所以每当我更改一个值时,我必须遍历所有类来更改每个页面的值

所以我的主要问题是,是否有可能将FormatPosts方法移动到一个名为LayoutUtils的C类中,因此在每个页面的构造函数上,我只使用LayoutUtils.FormatPosts,它返回所有解析的内容或其他内容,并将XML放在一个类中,因此如果我更改它,它会到处更新它

我使用的XML代码是

     <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage *snip*>
<ContentPage.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="TextPostTemplate">
            <ViewCell>
                <StackLayout BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 10, 10">
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding Name}" TextColor = "Black" FontSize = "15"/>  
                    </StackLayout>
                    <Label Text="{Binding Body}" TextColor = "Black"/>
                    <StackLayout Orientation="Horizontal" Padding="0, 0, 0, 5">
                        <Label Text="{Binding Likes}" FontSize="15" TextColor="Black" Margin="10, 0, 0, 0"/>
                        <Image Source="{Binding LikeSource}" HeightRequest = "22" HorizontalOptions = "StartAndExpand"/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
        <DataTemplate x:Key="InvalidPostTemplate">
            <ViewCell>
                <StackLayout BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 10, 10">
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding Name}" TextColor = "Black" FontSize = "15"/>
                    </StackLayout>
                    <Label Text="This post type is not supported in your current version!" TextColor = "Red" Margin="0, 10, 0, 10" HorizontalOptions="CenterAndExpand" HorizontalTextAlignment="Center"/>
                    <StackLayout Orientation="Horizontal" Padding="0, 0, 0, 5">
                        <Label Text="{Binding Likes}" FontSize="15" TextColor="Black" Margin="10, 0, 0, 0"/>
                        <Image Source="{Binding LikeSource}" HeightRequest = "22" HorizontalOptions = "StartAndExpand"/>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
        <local:TweetTemplateSelector x:Key="TweetTemplateSelector"
                         TextPostTemplate="{StaticResource TextPostTemplate}"
                         InvalidPostTemplate="{StaticResource InvalidPostTemplate}" />
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content >
    <StackLayout BackgroundColor="#1289A7">
        <ListView x:Name="PostListView" ItemsSource="{Binding PostObject}"
      ItemTemplate="{StaticResource TweetTemplateSelector}" HasUnevenRows="True" />
    </StackLayout>
</ContentPage.Content>
然后,我使用以下代码绑定XML

     public static IList<PostObject> FormatPosts(Page page, INavigation navigation, string json)
    {
        IList<PostObject> Posts = new List<PostObject>() { };
        var posts = JsonConvert.DeserializeObject<List<Post>>(json);

        foreach (var post in posts)
        {
            if (post.type == 0)
            {
                Posts.Add(new TextPost
                {
                    Name = post.name,
                    Body = post.body,
                    Likes = LayoutUtils.FormatCounter(post.Likes),
                    LikeSource = post.Liked == 1 ? "liked_icon.png" : "like_icon.png"
                });
            }
            else
            {
                Posts.Add(new InvalidPost
                {
                    Name = post.name,
                    Likes = LayoutUtils.FormatCounter(post.Likes),
                    LikeSource = post.Liked == 1 ? "liked_icon.png" : "like_icon.png"
                });
            }
        }
        return Posts;
    }

你有两个选择

您可以在当前使用上述代码的任何位置创建一个自定义代码,该代码基本上包含上述所有代码。这是推荐的方法,因为您将只在1个文件中管理与此视图相关的所有内容,从技术上讲,是2个xaml和cs代码 或者,您可以使用该类,并在其中添加上面创建的数据模板。这将允许你在你的应用程序中重复使用它们,就像你现在做的一样。这个选项的缺点是,您仍然需要每次编写所有代码。