我目前正在使用 Xamarin 中的一個應用程式,為 UWP 構建它。
在這個應用程式中,我有一個堆疊,其中有一張人體影像,上面覆寫了其他影像(彩色圓圈)。我想要做的是在身體上的圓圈頂部放置一個標簽,并根據我從另一個檔案中提取的 ObservableCollection(稱為 TagInfoList)中的資料實時更新它們。為了進行設定,我制作了一個 5x4 網格,身體影像占據了整個網格,并將圓圈放置在網格的各個單元格中。此外,標簽進入它們自己的單元格。我遇到的問題是讓我的標簽顯示并從我的 TagInfoList 主動更新。
這是我要設定的內容。我希望我的標簽位于彩色圓圈的頂部。
這是 XAML 代碼:
<StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand"> <!-- Body Model Section -->
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" WidthRequest="400" ColumnSpacing="0">
<Grid.ColumnDefinitions> <!-- 4 equal size Columns-->
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions> <!-- 5 equal size Rows -->
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Source="{pages:ImageResource BLE.Client.Pages.body.png}" Aspect="AspectFit" Grid.Row="0" Grid.Column="0" Grid.RowSpan="5" Grid.ColumnSpan="4"/>
<Image Source="{pages:ImageResource BLE.Client.Pages.green.png}" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Aspect="AspectFit" Opacity="0.6"/>
<Image Source="{pages:ImageResource BLE.Client.Pages.red.png}" Grid.Row="2" Grid.Column="0" Aspect="AspectFit" Opacity="0.6"/>
<Image Source="{pages:ImageResource BLE.Client.Pages.green.png}" Grid.Row="2" Grid.Column="3" Aspect="AspectFit" Opacity="0.6"/>
<Image Source="{pages:ImageResource BLE.Client.Pages.green.png}" Grid.Row="4" Grid.Column="1" Aspect="AspectFit" Opacity="0.6"/>
<Image Source="{pages:ImageResource BLE.Client.Pages.red.png}" Grid.Row="4" Grid.Column="2" Aspect="AspectFit" Opacity="0.6"/>
<CollectionView ItemsSource="{Binding TagInfoList}">
<CollectionView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding SensorAvgValue}" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Center" Grid.ColumnSpan="2"/>
<Label Grid.Row="2" Grid.Column="0" Text="{Binding SensorAvgValue}" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Grid.Row="2" Grid.Column="3" Text="{Binding SensorAvgValue}" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Grid.Row="4" Grid.Column="1" Text="{Binding SensorAvgValue}" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Grid.Row="4" Grid.Column="2" Text="{Binding SensorAvgValue}" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Center"/>
</ViewCell>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</StackLayout> <!-- Body Model Section -->
我嘗試將標簽放在 CollectionView 中,因為我可以將它系結到 TagInfoList,這是 Grid 無法做到的。但是,我不確定需要對物件的層次結構進行哪些更改才能使標簽主動更新。我呼叫的 TagInfoList 和 SensorAvgValue 都在其他 StackLayout 實體中正確更新,所以我知道 ObservableCollection 沒有問題。
此外,我下一步將根據標簽中的值更改影像,因此我可能還需要將影像物件移動到標簽所在的任何級別,以便它們可以相互互動。
如果在 .xaml.cs 檔案中更容易做到這一點,我也可以將代碼轉移到一個檔案中。一個存在,但它只存在于我的布局中不同堆疊的一部分。
任何和所有的建議將不勝感激。謝謝你們!
uj5u.com熱心網友回復:
因此,正如用戶 Jason 所說:模型會很好。反正...
如果您只想要一個 Image 和一個批次,請將其包裝成“ BatchView ”。
另一種簡單的方法是將所有 (Image, Label, Batch(?)) 放入“ AbsolutLayout ”然后定位它,因為 AbsolutLayout 就像一個疊加層。
最后將其全部設定為帶有OnPropertyChanged的?? Binding ,因此它會像在 CollectionView 中一樣更新自身 - 在 CollectionView 中,它是相同的機制,但是是自動化的。
只需繪制一個簡單的模型并發布;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461942.html
標籤:xml xamarin uwp uwp-xml xamarin.uwp
