簡體   English   中英

Xamarin 表單包含 xaml 文件

[英]Xamarin Forms include xaml file

我有一個寫標題的 xaml 文件。

<?xml version="1.0" encoding="UTF-8"?>                                                    
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="TeenageClubWallet.Templates.Header">
<StackLayout>
    <Label Text="Header!" />
</StackLayout>

我想將此 xaml 文件用作其他 xaml 文件中的標頭。 這是一個例子。 我正在嘗試將它包含到另一個 xaml 文件中:

我添加了一個命名空間:xmlns:template="clr-namespace:TeenageClubWallet.Templates"

“模板”標簽現在用於包含文件:

<?xml version="1.0" encoding="utf-8" ?>                                                   
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="TeenageClubWallet.Statistics"
         xmlns:template="clr-namespace:TeenageClubWallet.Templates">

<ContentPage.Content>
    <template:Header/>

    <StackLayout>
        <Label Text="Content!" />
    </StackLayout>
</ContentPage.Content>

Visual Studio 顯示錯誤:“錯誤 XLS0501 多次設置屬性‘內容’。”

如何將一個 xaml 文件中的內容包含到另一個 xaml 文件中?

在此示例中ContentPage有兩個元素 - template:HeaderStackLayout並且只允許使用一個。

這就是這個問題的原因。 我沒有更深入地評估是否還有其他問題,但是如果它們存在並且您無法解決它們,您應該提出一個新問題。

我修好了它

要解決此問題,您需要在<StackLayout>標簽內插入<template:Header/> <StackLayout>標簽。

下面是完整的代碼:

 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TeenageClubWallet.Statistics" xmlns:template="clr-namespace:TeenageClubWallet.Templates"> <ContentPage.Content> <StackLayout> <template:Header/> <Label Text="Content!" /> </StackLayout> </ContentPage.Content>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM