2015-12-20 48 views
0

我正在嘗試創建一個Windows 10應用程序,其中的設置頁面可讓用戶輸入應用程序用於連接到外部服務的憑據。我想將這些憑據存儲在PasswordVault中。將XAML控件綁定到複雜數據類型(PasswordCredential)

我用Template10 Template10作爲起點。我已將TextBox和PasswordBox添加到Settings PivotItem。我已將PasswordCredential成員添加到ISettingsService。並且,我向SettingsService類添加了一個實現,以便從Vault中存儲和檢索PasswordCredential對象。

現在,我需要將TextBox和PasswordBox連接到PasswordCredental對象的UserName和Password屬性。當更新用戶名/密碼對時,還需要執行一些邏輯。我是XAML的新手,我並不清楚如何才能正常工作。有什麼建議麼?

+0

如果您已經知道如何在XAML中綁定,請查看[IValueConverter接口](https://msdn.microsoft.com/it-it/library/windows/apps/windows.ui.xaml.data。 ivalueconverter) – fillobotto

+0

@FilippoB這對我來說是新的,但我試圖使用IValueConverter實現。但是,我似乎無法讓我在XAML中引用該類來解決這個問題。我試圖將Text的值設置爲「{Binding Path = PasswordCredentialObject,Converter = {StaticResource Helpers:PasswordCredentialConverter},ConverterParameter = UserName}」,並且我添加了xmlns:Helpers =的名稱空間引用using:WindowsApp1.Helpers 「,但它無法解決參考。 –

+0

找出參考問題。我忽略了在頁面的XAML中爲轉換器聲明靜態資源。 –

回答

1

這比您想象的要容易。假設這種模式:

public class UserCredentials : BindableBase 
{ 
    string _userName = default(string); 
    public string UserName { get { return _userName; } set { Set(ref _userName, value); } } 

    string _password = default(string); 
    public string Password { get { return _password; } set { Set(ref _password, value); } } 
} 

您可以在XAML做到這一點:

<!-- login form --> 
<StackPanel Grid.Row="1" Margin="20, 16" DataContext="{Binding ElementName=ThisPage}"> 
    <TextBox Header="Username" Text="{Binding UserCredentials.UserName, Mode=TwoWay}" /> 
    <TextBox Header="Password" Text="{Binding UserCredentials.Password, Mode=TwoWay}" /> 
    <Button Click="LoginClicked" Margin="0,12,0,0" HorizontalAlignment="Right">Login</Button> 
</StackPanel> 

有意義嗎?爲了幫助,我將其添加到Template 10的GitHub倉庫中的Sample項目中,如果你想看到它的工作。

祝你好運。