Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 视图中的视图未更新-Caliburn.Micro_C#_Mvvm_Caliburn.micro - Fatal编程技术网

C# 视图中的视图未更新-Caliburn.Micro

C# 视图中的视图未更新-Caliburn.Micro,c#,mvvm,caliburn.micro,C#,Mvvm,Caliburn.micro,我遇到了一个问题,datagrid在连接到视图中的视图时没有反映对其集合的更改。更准确地说,我在主视图中有第二个视图。在第二个视图中,我有一个datagrid,其autogeneratecolumns设置为true;首次呈现datagrid时,它将显示相应的列和标题。但是,当我填充附加到它的列表时,不会反映任何更改 以下是两个视图及其各自视图模型的完整代码: 主窗口视图: <Window x:Class="MyApp.MainWindowView" xmlns="http://sc

我遇到了一个问题,datagrid在连接到视图中的视图时没有反映对其集合的更改。更准确地说,我在主视图中有第二个视图。在第二个视图中,我有一个datagrid,其autogeneratecolumns设置为true;首次呈现datagrid时,它将显示相应的列和标题。但是,当我填充附加到它的列表时,不会反映任何更改

以下是两个视图及其各自视图模型的完整代码: 主窗口视图:

<Window x:Class="MyApp.MainWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:cal="http://www.caliburnproject.org"
    xmlns:views="clr-namespace:MyApp"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindowView" Height="300" Width="300">
<Grid>
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="File">
                <MenuItem Header="Open" x:Name="Open"/>
                <MenuItem Header="Exit" x:Name="Exit"/>
            </MenuItem>
        </Menu>
        <StackPanel DockPanel.Dock="Bottom">
            <views:SecondView/>
        </StackPanel>
    </DockPanel>
</Grid>
<UserControl x:Class="MyApp.SecondView"
         xmlns:cal="http://www.caliburnproject.org"
         cal:Bind.Model="MyApp.SecondViewModel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <StackPanel>
        <DataGrid x:Name="MyList"/>
    </StackPanel>
</Grid>

MainWindowViewModel:

namespace MyApp
{
[Export(typeof(IShell))]
internal class MainWindowViewModel : Screen, IShell
{
    Regex expression = new Regex(@"^N\d\.C\d\.D\d\.R\d:\s\s\s-\d"); //ex. "N1.C1.D2.R1:   -3"        
    SecondViewModel svm = new SecondViewModel();        
    public void Open()
    {
        Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog();
        openFile.Multiselect = true;
        openFile.Filter = "Text Files(*.txt)|*.txt|Log Files(*.log)|*.log|All Files(*.*)|*.*";
        openFile.Title = "Open File(s)";
        bool? userClickedOK = openFile.ShowDialog();
        string[] _fileNames = openFile.FileNames;
        if (userClickedOK == true)
        {
            if (_fileNames != null)
            {
                for (int i = 0; i < _fileNames.Length; i++)
                {
                    ValidFiles(_fileNames[i]);
                }
            }
        }
    }
    public void Exit()
    {
        App.Current.Shutdown();
    }
    /* ValidFiles() accepts a string containing a filename and creates a Streamreader that reads the file if it is not a Boxboro file.
     */
    public void ValidFiles(string filename)
    {
        string line;
        using (StreamReader sr = new StreamReader(filename))
        {
            while ((line = sr.ReadLine()) != null)
            {
                if (line.Contains("Mono Status"))
                {
                    Console.WriteLine("File(s) not supported by this parser. Please select a valid file.");
                    break;
                }
                else
                {
                    IsMatch(line);
                }
            }
        }
    }
    /* IsMatch() accepts a string "input" and determines which parsing method to send the string to, if any.
     * Strings not matching any of the initial criteria are not processed to limit overhead.
     */
    public void IsMatch(string input)
    {
        Match match = expression.Match(input);
        if (match.Success)
        {
            svm.GetData(input);
        }
    }
}
namespace MyApp
{
[Export(typeof(SecondViewModel))]
class SecondViewModel:Screen
{
    Parse parse = new Parse();
    BindableCollection<MyObject> myList = new BindableCollection<MyObject>();
    MyObject myObject;
    public MyObject MyObject
    {
        get { return myObject; }
        set
        {
            myObject = value;
            NotifyOfPropertyChange(() => MyList);
        }
    }
    public BindableCollection<MyObject> MyList
    {
        get { return myList; }
        set 
        { 
            MyList = value;
            NotifyOfPropertyChange(() => MyList);
        }
    }
    public void GetData(string input)
    {
        string[] tempArray = input.Split();
        List<int> tempList = new List<int>();
        for (int i = 1; i < tempArray.Length; i++)
        {
            if (!string.IsNullOrEmpty(tempArray[i]))
            {
                tempList.Add(Convert.ToInt32(tempArray[i]));
            }
        }
        int[] tempIntArray = tempList.ToArray();
        MyObject = new MyObject(tempArray[0], tempIntArray[0], tempIntArray[1], tempIntArray[2], tempIntArray[3]);
        this.MyList.Add(MyObject);

        Console.WriteLine("MyList has " + MyList.Count.ToString() + " elements.");
        //foreach (MyObject item in MyList)
        //{
        //    Console.WriteLine(item.Location);
        //}
    }
}
名称空间MyApp
{
[导出(类型(IShell))]
内部类MainWindowViewModel:屏幕,IShell
{
正则表达式=新正则表达式(@“^N\d\.C\d\.d\d\.R\d:\s\s-\d”);//例如“N1.C1.D2.R1:-3”
SecondViewModel svm=新的SecondViewModel();
公开作废
{
Microsoft.Win32.OpenFileDialog openFile=新建Microsoft.Win32.OpenFileDialog();
openFile.Multiselect=true;
openFile.Filter=“文本文件(*.txt)|*.txt |日志文件(*.Log)|*.Log |所有文件(*.*)|*.*”;
openFile.Title=“打开文件”;
bool?userClickedOK=openFile.ShowDialog();
字符串[]_fileNames=openFile.fileNames;
if(userClickedOK==true)
{
如果(_fileNames!=null)
{
对于(int i=0;i<\u fileNames.Length;i++)
{
有效文件(_文件名[i]);
}
}
}
}
公共无效出口()
{
App.Current.Shutdown();
}
/*ValidFiles()接受包含文件名的字符串,并创建一个Streamreader,如果该文件不是Boxboro文件,它将读取该文件。
*/
公共无效有效文件(字符串文件名)
{
弦线;
使用(StreamReader sr=新StreamReader(文件名))
{
而((line=sr.ReadLine())!=null)
{
if(行包含(“单声道状态”))
{
Console.WriteLine(“此解析器不支持文件。请选择有效文件”);
打破
}
其他的
{
IsMatch(线路);
}
}
}
}
/*IsMatch()接受字符串“输入”,并确定将字符串发送到哪个解析方法(如果有)。
*不匹配任何初始条件的字符串不会被处理以限制开销。
*/
public void IsMatch(字符串输入)
{
匹配=表达式。匹配(输入);
如果(匹配成功)
{
获取数据(输入);
}
}
}
}

第二窗口视图:

<Window x:Class="MyApp.MainWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:cal="http://www.caliburnproject.org"
    xmlns:views="clr-namespace:MyApp"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindowView" Height="300" Width="300">
<Grid>
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="File">
                <MenuItem Header="Open" x:Name="Open"/>
                <MenuItem Header="Exit" x:Name="Exit"/>
            </MenuItem>
        </Menu>
        <StackPanel DockPanel.Dock="Bottom">
            <views:SecondView/>
        </StackPanel>
    </DockPanel>
</Grid>
<UserControl x:Class="MyApp.SecondView"
         xmlns:cal="http://www.caliburnproject.org"
         cal:Bind.Model="MyApp.SecondViewModel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <StackPanel>
        <DataGrid x:Name="MyList"/>
    </StackPanel>
</Grid>

SecondWindowViewModel:

namespace MyApp
{
[Export(typeof(IShell))]
internal class MainWindowViewModel : Screen, IShell
{
    Regex expression = new Regex(@"^N\d\.C\d\.D\d\.R\d:\s\s\s-\d"); //ex. "N1.C1.D2.R1:   -3"        
    SecondViewModel svm = new SecondViewModel();        
    public void Open()
    {
        Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog();
        openFile.Multiselect = true;
        openFile.Filter = "Text Files(*.txt)|*.txt|Log Files(*.log)|*.log|All Files(*.*)|*.*";
        openFile.Title = "Open File(s)";
        bool? userClickedOK = openFile.ShowDialog();
        string[] _fileNames = openFile.FileNames;
        if (userClickedOK == true)
        {
            if (_fileNames != null)
            {
                for (int i = 0; i < _fileNames.Length; i++)
                {
                    ValidFiles(_fileNames[i]);
                }
            }
        }
    }
    public void Exit()
    {
        App.Current.Shutdown();
    }
    /* ValidFiles() accepts a string containing a filename and creates a Streamreader that reads the file if it is not a Boxboro file.
     */
    public void ValidFiles(string filename)
    {
        string line;
        using (StreamReader sr = new StreamReader(filename))
        {
            while ((line = sr.ReadLine()) != null)
            {
                if (line.Contains("Mono Status"))
                {
                    Console.WriteLine("File(s) not supported by this parser. Please select a valid file.");
                    break;
                }
                else
                {
                    IsMatch(line);
                }
            }
        }
    }
    /* IsMatch() accepts a string "input" and determines which parsing method to send the string to, if any.
     * Strings not matching any of the initial criteria are not processed to limit overhead.
     */
    public void IsMatch(string input)
    {
        Match match = expression.Match(input);
        if (match.Success)
        {
            svm.GetData(input);
        }
    }
}
namespace MyApp
{
[Export(typeof(SecondViewModel))]
class SecondViewModel:Screen
{
    Parse parse = new Parse();
    BindableCollection<MyObject> myList = new BindableCollection<MyObject>();
    MyObject myObject;
    public MyObject MyObject
    {
        get { return myObject; }
        set
        {
            myObject = value;
            NotifyOfPropertyChange(() => MyList);
        }
    }
    public BindableCollection<MyObject> MyList
    {
        get { return myList; }
        set 
        { 
            MyList = value;
            NotifyOfPropertyChange(() => MyList);
        }
    }
    public void GetData(string input)
    {
        string[] tempArray = input.Split();
        List<int> tempList = new List<int>();
        for (int i = 1; i < tempArray.Length; i++)
        {
            if (!string.IsNullOrEmpty(tempArray[i]))
            {
                tempList.Add(Convert.ToInt32(tempArray[i]));
            }
        }
        int[] tempIntArray = tempList.ToArray();
        MyObject = new MyObject(tempArray[0], tempIntArray[0], tempIntArray[1], tempIntArray[2], tempIntArray[3]);
        this.MyList.Add(MyObject);

        Console.WriteLine("MyList has " + MyList.Count.ToString() + " elements.");
        //foreach (MyObject item in MyList)
        //{
        //    Console.WriteLine(item.Location);
        //}
    }
}
名称空间MyApp
{
[导出(类型(SecondViewModel))]
第二类视图模型:屏幕
{
Parse Parse=新Parse();
BindableCollection myList=新建BindableCollection();
肌体肌体;
公共MyObject MyObject
{
获取{return myObject;}
设置
{
myObject=值;
NotifyOfPropertyChange(()=>MyList);
}
}
公共BindableCollection MyList
{
获取{return myList;}
设置
{ 
MyList=值;
NotifyOfPropertyChange(()=>MyList);
}
}
public void GetData(字符串输入)
{
字符串[]tempArray=input.Split();
列表模板列表=新建列表();
for(int i=1;i
}

助推器:

namespace MyApp
{
    internal class AppBootStrapper : Bootstrapper<IShell>
    {
        static AppBootStrapper()
        {
            //Initializes the logger for debugging, remove or comment out in release.
            LogManager.GetLog = type => new DebugLogger(type);
        }
        private CompositionContainer container;
        protected override void BuildUp(object instance)
        {
            this.container.SatisfyImportsOnce(instance);
        }
        protected override void Configure()
        {
            this.container = 
                new CompositionContainer(
                    new AggregateCatalog(
                        AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()));
            var batch = new CompositionBatch();
            batch.AddExportedValue<IWindowManager>(new WindowManager());
            batch.AddExportedValue<IEventAggregator>(new EventAggregator());
            batch.AddExportedValue(this.container);
            this.container.Compose(batch);
        }
        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return this.container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }
        //This method is required for the BootStrapper.cs to be discovered.
        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            IEnumerable<object> exports = this.container.GetExportedValues<object>(contract);
            if (exports.Count() > 0)
            {
                return exports.First();
            }
            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
        }
    }
}
名称空间MyApp
{
内部类AppBootStrapper:引导程序
{
静态AppBootStrapper()
{
//初始化记录器以在版本中调试、删除或注释。
LogManager.GetLog=type=>newdebuglogger(type);
}
专用合成容器;
受保护的覆盖空洞堆积(对象实例)
{
this.container.SatisfyImportsOnce(实例);
}
受保护的覆盖无效配置()
{
此.container=
新合成容器(
新AggregateCatalog(
Select(x=>newAssemblyCatalog(x)).OfType();
var batch=新的CompositionBatch();
AddExportedValue(新的WindowManager());
AddExportedValue(新的EventAggregator());
batch.AddExportedValue(this.container);
此.container.Compose(批处理);
}
受保护的重写IEnumerable GetAllInstances(类型serviceType)
{
返回此.container.GetExportedValues(AttributedModelServices.GetContractName(serviceType));
}
//查找BootStrapper.cs需要此方法。
受保护的覆盖对象GetInstance(类型serviceType,字符串键)
{
string contract=string.IsNullOrEmpty(key)?AttributedModelServices.GetContractName(serviceType):key;
IEnumerable exports=this.container.GetExportedValues(合同);
如果(exports.Count()>0)
{
返回导出;
}
引发新异常(string.Format(“