我一直在努力讓系結在邊界內作業。當我嘗試在邊界外系結某些東西時,它作業得很好,但我無法讓它在邊界內作業。
這里的代碼有效:
<DataTemplate x:Key="TaskTemplate">
<GridLayout
ColumnDefinitions="*"
>
<Label Text={Binding Path=TestText}/>
</GridLayout>
</DataTemplate>
此代碼正確顯示文本。
什么不是,我需要什么才能上班:
<DataTemplate x:Key="TaskTemplate">
<GridLayout
ColumnDefinitions="*"
>
<Border
GridLayout.Column="0"
BackgroundColor="{StaticResource MidBackgroundColor}"
StrokeThickness="1"
Stroke="Transparent"
HorizontalOptions="Center">
<Border.StrokeShape>
<RoundRectangle CornerRadius="30"/>
</Border.StrokeShape>
<Label
Text="{Binding Path=TestText}"/>
</Border>
</GridLayout>
</DataTemplate>
此代碼將其顯示為空。
根據我到目前為止的理解,Border 帶走了 Context,我已經嘗試了很多其他執行緒的東西來讓它作業,但由于我很新,我正在努力找出它是如何作業的確切地。
提前致謝!
編輯:我已經嘗試用純文本替換 Binding 并且效果很好,所以并不是說 Border 中的 Label 是不可見的。
我想澄清一下,前面提到的 Text 屬性實際上不是 Label 或 Border 的 Text 屬性,而是有界物件的 Text 屬性。為清楚起見,將其重命名為 TestText。
此代碼顯示正確的文本:
<GridLayout
ColumnDefinitions="*"
>
<Border
GridLayout.Column="0"
BackgroundColor="{StaticResource MidBackgroundColor}"
StrokeThickness="1"
Stroke="Transparent"
HorizontalOptions="Center">
<Border.StrokeShape>
<RoundRectangle CornerRadius="30"/>
</Border.StrokeShape>
<Label
Text="Testing text."/>
</Border>
</GridLayout>
</DataTemplate>
想要的結果是這樣的:https : //i.stack.imgur.com/wUdGD.jpg
但不是硬編碼文本,它應該系結另一個物件的文本。這不應該是其他代碼的問題,因為如果我在 Border 之外撰寫代碼它就可以正常作業。
EDIT2:包括所有其他代碼。
這是顯示所有內容的頁面。
任務頁面.xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage.Resources>
<ResourceDictionary Source="/Views/ViewTask.xaml"/>
</ContentPage.Resources>
<GridLayout RowDefinitions="*">
<GridLayout
GridLayout.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<StackLayout Margin="5,5,5,0">
<CollectionView x:Name="TasksCollection"
ItemsSource="{Binding Tasks}"
ItemTemplate="{StaticResource TaskTemplate}">
</CollectionView>
</StackLayout>
</GridLayout>
</GridLayout>
</ContentPage>
任務頁面.xaml.cs:
public partial class TasksPage : ContentPage
{
public TasksPage()
{
InitializeComponent();
BindingContext = new TaskViewModel();
}
}
這是該集合的專案模板。視圖任務.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<DataTemplate x:Key="TaskTemplate">
<GridLayout
ColumnDefinitions="*">
<Border
GridLayout.Column="0"
BackgroundColor="{StaticResource MidBackgroundColor}"
StrokeThickness="1"
Stroke="Transparent"
HorizontalOptions="Center">
<Border.StrokeShape>
<RoundRectangle CornerRadius="30"/>
</Border.StrokeShape>
<Label GridLayout.Column="0"
GridLayout.Row="0"
Text="{Binding TaskText}" />
</Border>
</GridLayout>
</DataTemplate>
</ResourceDictionary>
The ViewModel just has an ObservableCollection, which works just fine. So all the items actually populate the DataTemplate, it's just that the DataTemplate only shows Binded properties if it's not within a Border.
uj5u.com熱心網友回復:
其他人也認為這是一個錯誤。萊斯特·莫雷諾 (Lester Moreno) 在宣布毛伊島預覽版 9 的博客上發表評論。
現在,您可以Border使用“Frame-inside-Frame”進行模擬:
<!-- Without Grid, the Frame takes the whole width. -->
<!-- To have multiple buttons in one row, use more Grid columns. -->
<!-- Border will be more convenient once it works. -->
<Grid ColumnDefinitions="Auto,*">
<!-- "Padding 6" of outer frame determines how thick the Border color is. -->
<Frame HasShadow="False" CornerRadius="18" Padding="6" BackgroundColor="#F69927">
<!-- "Padding 2" of inner frame is white space surrounding the label. -->
<Frame HasShadow="False" CornerRadius="12" BackgroundColor="White" Padding="6,2">
<Label Text="{Binding LabelText}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
TextColor="Blue"/>
</Frame>
</Frame>
</Grid>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/399701.html
標籤:wpf xaml binding border maui
