如何在MVVM模式中实现OpenFileDialog WPF代码

问题描述

我想以MVVM模式在WPF应用程序中实现OpenFileDialog。我添加一个接口模型类。但是不知道如何正确实施。

Mainwindow.xaml

<Window x:Class="FileManager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FileManager"
        mc:Ignorable="d"
         Style="{StaticResource RedStyle}"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
    <TextBlock Text="ExFile Manager Application" HorizontalAlignment="Center" Margin="20,5,20,0" />
<TextBox HorizontalAlignment="Center" Height="43" Margin="30,35,0" textwrapping="Wrap"   
                 Text="Select only the text file" VerticalAlignment="Top" Width="476" Name="FileNameTextBox"/>  
        <Button x:Name="browseButton"  Content="browse a file" HorizontalAlignment="Right"  Click="browseButton_Click"  
                Margin="485,10,0" VerticalAlignment="Top" Width="121"  
                RenderTransformOrigin="1.047,0.821" Height="40"/>  
        <TextBlock HorizontalAlignment="Left" Height="282" Margin="30,96,0"   
                   textwrapping="Wrap" VerticalAlignment="Top"  
                   Width="703" Name="TextBlock1"/>  
    </Grid>
</Window>

Mainwindow.cs

namespace FileManager
{
   
    public partial class MainWindow : Window
    {
        private IOpenFileService _IOpenFileService;
        public MainWindow()
        {
            InitializeComponent();
        }
        public string FileNames{
        get { return _IOpenFileService.FileName;}
        set {_IOpenFileService.FileName=value; }
        
        }
        public string result{
            get{return _IOpenFileService.result;}
            set{_IOpenFileService.result=value;}
        }
        private void browseButton_Click(object sender,RoutedEventArgs e)
        {
            // Create OpenFileDialog

            Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
            // Set filter for file extension and default file extension  
            openFileDlg.DefaultExt = ".txt";
            openFileDlg.Filter = "Text documents (.txt)|*.txt";

            // Launch OpenFileDialog by calling ShowDialog method
            //specifies whether the activity was accepted (true) or canceled (false).
            //ShowDialog() is called on a window that is closing (Closing) or has been closed (Closed).
            Nullable<bool> result = openFileDlg.ShowDialog();
            // Get the selected file name and display in a TextBox.
            // Load content of file in a TextBlock
            if (result == true)
            {


                FileNameTextBox.Text = openFileDlg.FileName;
                TextBlock1.Text = System.IO.File.ReadAllText(openFileDlg.FileName);
            }
            
        }
    }
}

Model.cs

namespace FileManagerModel.Model
{
    public interface IOpenFileService
    {
        string FileName { get; set;}
         string result  {get; set;}
        
    }
}

我已经将 XAML 文件 XAML.cs 文件与此一起附加了

我是dotnet核心和WPf应用程序的新手。另外,请检查我的编码样式和结构。我也想以MVVM模式实现这一点。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...