我正在创建一个UWP应用程序,我希望用户能够上传照片。我有一个ContentDialog和一个Button和两个TextBoxes。当用户按下“上传照片” ContentDialog时,Button应该会弹出。如何使用MVVM执行此操作?

查找文件并将文件发布到数据库的逻辑已经完成,仅剩下UI。

这是我的XAML:

<!-- Content -->
<Button Content="Upload a photo to gallery" Margin="40,0,0,0" x:Name="UploadPhotoButton" Command="{x:Bind MyProfileViewModel.OpenContentDialog}"/>
<!-- More content -->

<!-- This is the ContentDialog I want to display when the user presses the button above -->
<ContentDialog x:Name="UploadPhotoContentDialog"
    PrimaryButtonText="Upload" IsPrimaryButtonEnabled="{Binding IsValid}"
    SecondaryButtonText="Cancel" IsSecondaryButtonEnabled="True">
    <ContentDialog.TitleTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <SymbolIcon Symbol="BrowsePhotos"/>
                <TextBlock Margin="10,0,0,0" Text="Upload photo "/>
            </StackPanel>
        </DataTemplate>
    </ContentDialog.TitleTemplate>

    <Grid Padding="10" Margin="0,10,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Photo Title:" VerticalAlignment="Center"/>
        <TextBox Text="{Binding PhotoTitle, Mode=TwoWay}" PlaceholderText="Example: Fun in the sun" Grid.Column="1"/>
        <TextBlock Text="Photo Caption:" Grid.Row="1" VerticalAlignment="Center"/>
        <TextBox Text="{Binding PhotoDesc, Mode=TwoWay}" PlaceholderText="Example: Don't you just love the beach" Grid.Row="1" Grid.Column="1"/>
        <Button Content="Browse files..." Grid.Column="0" Grid.Row="2" Margin="0,20,0,0" Command="{x:Bind MyProfileViewModel.FindFile}"/>
        <TextBox Text="{Binding FileName, Mode=TwoWay}" Grid.Row="2" Grid.Column="1" Margin="10,20,0,0" FontSize="12" Height="32" IsReadOnly="True" />
    </Grid>
</ContentDialog>

到目前为止,这是我的C#文件(MyProfileViewModel):
public ICommand OpenContentDialog => new CommandHandler(async () => {
    //  What to put in here to find the ContentDialog, then display it?
});

最佳答案

方法之一可以传递ContentDialog作为CommandParameter。例如这样:

<Button Content="Upload a photo to gallery" Margin="40,0,0,0" x:Name="UploadPhotoButton" Command="{x:Bind MyProfileViewModel.OpenContentDialog}" CommandParameter="{Binding ElementName=UploadPhotoContentDialog}"/>

和发票:

public RelayCommand OpenContentDialog => new RelayCommand(async (dialog) => { (dialog as ContentDialog).ShowAsync(); });

关于c# - 使用ICommand和MVVM在UWP中显示ContentDialog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47613293/

10-12 03:20