我有以下 xaml 代碼:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Check In"
Clicked="CheckInFile"/>
<MenuItem Text="Check Out"
Clicked="CheckOutFile"/>
<MenuItem Text="Download"
Clicked="DownloadFile"/>
<MenuItem Text="Upload Local Copy"
Clicked="UploadFile"/>
</ViewCell.ContextActions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label Grid.Column="0" Text="{Binding Path=DriveItem.Name}"
FontSize="Small" />
<Label Grid.Column="1""
FontFamily="Segoe MDL2 Assets"
Text="{Binding PublicationStatus}"
/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
以及相關的cs代碼:
protected override async void OnAppearing()
{
var directoryContents = await App.GraphClient.Sites[siteID].Lists[sharedDocsDriveId]
.Items
.Request()
//.Expand(item => item.DriveItem)
.Expand("driveItem($select=publication,name,id,description,file,webUrl)")
.GetAsync();
SharedDocumentList.ItemsSource = directoryContents.CurrentPage.ToList();
}
對于回傳的每個專案,它包含有關檔案的資訊 - 如果它目前已簽出或未簽出。該欄位為“級別”,如下所示/
directoryContents.CurrentPage[i].DriveItem.Publication
{Microsoft.Graph.PublicationFacet}
AdditionalData: null
Level: "checkout"
ODataType: null
VersionId: "4.0"
如果 Level 的值為“checkout”,我想使用這個顯示鎖定的圖示:https : //docs.microsoft.com/en-us/windows/apps/design/style/segoe-ui-symbol-font 但如果它是“已發布”,例如,我想使用解鎖圖示。
Is there a simple way to do some sort of IF statement within the XAML to evaluate the value of DriveItem.Publication.Level and then display the appropriate icon? I started to go down the route of exposing a new property in my codebehind called publicationStatus but ... maybe that's overkill. I feel like there's a simpler way I'm not thinking of?
EDIT 1
So I've updated the xaml to use the datatrigger method but I'm getting the error that the property "RelativeSource" was not found in type BindingExtension.
<ListView x:Name="SharedDocumentList"
HasUnevenRows="true"
>
<ListView.Resources>
<Style x:Key="PublicationStatus" TargetType="Label">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"
Value="checkout">
<Setter Property="Text" Value="\uE701"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"
Value="published">
<Setter Property="Text" Value="\uE702"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Check In"
Clicked="CheckInFile"/>
<MenuItem Text="Check Out"
Clicked="CheckOutFile"/>
<MenuItem Text="Download"
Clicked="DownloadFile"/>
<MenuItem Text="Upload Local Copy"
Clicked="UploadFile"/>
</ViewCell.ContextActions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label Grid.Column="0" Text="{Binding Path=DriveItem.Name}"
FontSize="Small" />
<Label Grid.Column="1"
Style="{StaticResource PublicationStatus}"
Tag="{Binding PublicationStatus}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
EDIT 2
This is my attempt at using the ValueConverter.
Here's what the code behind for the class looks like in part:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SPDocumentLibraryContentsPage : ContentPage
{
public string IconValue { get; set; }
public SPDocumentLibraryContentsPage()
{
InitializeComponent();
IconValue = "checkout";
//this.DataContext doesn't exist.
}
The ListView on the Xaml page looks like this:
<ListView.ItemTemplate> <ViewCell.ContextActions> </ViewCell.ContextActions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Label Grid.Column="0" Text="{Binding Path=DriveItem.Name}"
FontSize="Small" />
<Label x:Name="testBlock" Grid.Column="2"
Text="{Binding IconValue,Converter={StaticResource IconValueConverter}}" FontFamily="Segoe MDL2 Assets" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
而且我能夠毫無問題地創建 IconConverter 類。這里只是一個片段:
namespace GraphTutorial.Models
{
public class IconValueConverter : Xamarin.Forms.IValueConverter
{
}
uj5u.com熱心網友回復:
看起來你的應用是 Xamarin UWP 應用,而不是本機 UWP 應用。
您也可以嘗試在使用系結時使用ValueConverter。您需要做的是創建一個 ValueConverter 類,并在 ValueConverter 中創建您自己的將不同字串轉換為不同圖示的邏輯。之后,您只需要在系結中使用 ValueConverter 作為引數。
這是我在本機 UWP 應用中測驗過的示例代碼。它應該在 Xamarin UWP 應用中作業。您需要替換Textblock為Label.
代碼隱藏:
public class IconValueConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
string OutputValue ;
string textvalue = value as string;
if (textvalue.Equals("checkout"))
{
OutputValue = "\uE701";
}
else if (textvalue.Equals("published"))
{
OutputValue = "\uE702";
}
else
{
OutputValue = "\uE703";
}
return OutputValue;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
主頁:
public string IconValue { get; set;}
public MainPage()
{
this.InitializeComponent();
IconValue = "checkout";
this.DataContext = this;
}
Xml代碼:
<Page.Resources>
<local:IconValueConverter x:Key="IconValueConverter"/>
</Page.Resources>
<Grid>
<TextBlock x:Name="MyTextBlcok" Text="{Binding IconValue,Converter={StaticResource IconValueConverter}}" FontFamily="Segoe MDL2 Assets" />
</Grid>
uj5u.com熱心網友回復:
有一個聰明的方式通過實作這個Trigger和Tag財產避免使用IValueConverter或獲得訪問父DataContext從里面Style。
在串列視圖或根元素內
<ListView.Resources>
<Style x:Key="PublicationStatus" TargetType="Label">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets" />
<Style.Triggers>
<Trigger Property="Tag"
Value="checkout">
<Setter Property="Text" Value="\uE701"/>
</Trigger>
<Trigger Property="Tag"
Value="published">
<Setter Property="Text" Value="\uE702"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
然后在代碼中:
<Label Grid.Column="1"
Style="{StaticResource PublicationStatus}"
Tag="{Binding PublicationStatus}" />
您也可以重用一個樣式并將其放入 some ResourceDictionary.
警告。未測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/334799.html
下一篇:無法理解這個遞回函式是如何計算的
