- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面是一个最小的例子,我不可能再减少它了。
我在 ViewModel 中创建一个实时过滤的 CollectionView,如下所示:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;
namespace AntiBonto.ViewModel
{
[Serializable]
public class Person
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public string Name { get; set; }
public override string ToString()
{
return Name;
}
private int num;
public int Num
{
get { return num; }
set { num = value; RaisePropertyChanged(); }
}
}
class ObservableCollection2<T> : ObservableCollection<T>
{
public ObservableCollection2() : base() { }
public ObservableCollection2(T[] t) : base(t) { }
public void AddRange(IEnumerable<T> collection)
{
foreach (var i in collection)
{
Items.Add(i);
}
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
class MainWindow: ViewModelBase
{
public MainWindow() { }
private ObservableCollection2<Person> people = new ObservableCollection2<Person>();
public ObservableCollection2<Person> People
{
get
{
return people;
}
set
{
people = value;
RaisePropertyChanged();
}
}
public ICollectionView Team
{
get
{
CollectionViewSource cvs = new CollectionViewSource { Source = People, IsLiveFilteringRequested = true, LiveFilteringProperties = { "Num" } };
cvs.View.Filter = p => ((Person)p).Num != 11;
return cvs.View;
}
}
public ICollectionView Ujoncok
{
get
{
CollectionViewSource cvs = new CollectionViewSource { Source = People, IsLiveFilteringRequested = true, LiveFilteringProperties = { "Num" } };
cvs.View.Filter = p => ((Person)p).Num == 11;
return cvs.View;
}
}
}
}
GUI 有一个按钮可以修改 People 集合中的 Person 对象:
<Window x:Class="AntiBonto.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:vm="clr-namespace:AntiBonto.ViewModel"
mc:Ignorable="d"
Title="AntiBonto" Width="1024" Height="768">
<Window.DataContext>
<vm:MainWindow/>
</Window.DataContext>
<Window.Resources>
<FrameworkElement x:Key="DataContextProxy" DataContext="{Binding}"/> <!-- workaround, see http://stackoverflow.com/questions/7660967 -->
</Window.Resources>
<TabControl>
<TabItem Header="Tab2">
<StackPanel>
<Button Content="Does" Click="Button_Click"/>
<ContentControl Visibility="Collapsed" Content="{StaticResource DataContextProxy}"/>
<!-- workaround part 2 -->
<DataGrid ItemsSource="{Binding Ujoncok}" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Who" ItemsSource="{Binding DataContext.Team, Source={StaticResource DataContextProxy}, Mode=OneWay}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</TabItem>
</TabControl>
</Window>
我像这样从 XML 文件加载数据:
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Xml.Serialization;
namespace AntiBonto
{
[Serializable]
public class AppData
{
public Person[] Persons;
}
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private string filepath = "state.xml";
private AppData AppData
{
get { return new AppData { Persons = viewModel.People.ToArray()}; }
set { viewModel.People.AddRange(value.Persons);}
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var xs = new XmlSerializer(typeof(AppData));
if (File.Exists(filepath))
{
using (var file = new StreamReader(filepath))
{
AppData = (AppData)xs.Deserialize(file);
}
}
}
private ViewModel.MainWindow viewModel { get { return (ViewModel.MainWindow)DataContext; } }
private void Button_Click(object sender, RoutedEventArgs e)
{
Person p = viewModel.People.First(q => q.Name == "Ferencz Katalin");
if (p.Num == 11)
p.Num = 0;
else
p.Num= 11;
}
}
}
XML 文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<AppData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Persons>
<Person>
<Name>Person1</Name>
<Num>0</Num>
</Person>
<Person>
<Name>Person2</Name>
<Num>0</Num>
</Person>
</Persons>
</AppData>
当我点击按钮一次或两次时,我得到了一个NullReference
异常。没有内在的异常(exception)。异常不是出现在我的代码中,而是出现在框架代码中,所以它没有显示来源,我无法找出哪个对象为空以及异常来自哪里。我没有设法设置“进入 .NET 源代码”,它仍然告诉我没有可用的源代码。
这是堆栈跟踪:
at System.Windows.Data.ListCollectionView.RestoreLiveShaping() at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at AntiBonto.App.Main() in D:\Marci\Programozás\AntiBonto\AntiBonto\obj\Debug\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
最佳答案
我不知道为什么,但这修复了错误:
public ICollectionView Team
{
get
{
CollectionViewSource cvs = new CollectionViewSource { Source = People, IsLiveFilteringRequested = true, LiveFilteringProperties = { "Num" } };
cvs.View.Filter = p => ((Person)p).Num != 11;
cvs.View.CollectionChanged += EmptyEventHandler;
return cvs.View;
}
}
private void EmptyEventHandler(object sender, NotifyCollectionChangedEventArgs e) { }
我试图调试异常发生的地方,我想在集合发生变化时设置一个断点。订阅事件使异常消失。
关于c# - PresentationFramework 中的 NullReference 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394151/
为什么 System.Windows.Clipboard(PresentationCore.dll) 对System.Windows.Thickness (PresentationFramework.
当我将 WPF 应用程序部署到另一个用户的计算机时,出现以下异常: An unhandled exception of type 'System.Windows.Markup.XamlParseExc
我写的一个应用程序崩溃了,在事件查看器中我发现了以下内容: Faulting module name: PresentationFramework.ni.dll, version: 4.0.30319
下面是一个最小的例子,我不可能再减少它了。 我在 ViewModel 中创建一个实时过滤的 CollectionView,如下所示: using System.Collections.Generic;
我编写的一个应用程序崩溃了,在事件查看器中我发现了以下内容: Faulting module name: PresentationFramework.ni.dll, version: 4.0.3031
我正在尝试在 Visual Studio 2013 中加载解决方案,但我收到了这条消息: 当我单击“确定”时,它会显示另一条错误消息: Attempted re-targeting of the pr
我遇到了奇怪的编译时错误: 项目文件必须在引用列表中包含 .NET Framework 程序集“WindowsBase、PresentationCore、PresentationFramework”。
实际上很难在网上搜索类似的案例。所以我决定在这里问一下。 在我的测试项目中,我的 SUT 引用了 PresentationFramework来自 Assembly引用标签,目标框架是.NET Fram
我正在与一个奇怪的 NullReferenceException 作斗争,它显然是从 GetNameCore() 触发的ItemAutomationPeer的功能类(class)。 异常的详细信息如下
我正在开发WPF应用程序,该应用程序首先针对3.0框架。当我尝试使其在 4.0 上运行时,出现以下异常。 System.IO.FileNotFoundException was unhandled M
我想将 PresentationFramework.Aero 主题添加到我的 ResourceDictionary。ResourceDictionary 本身位于一个名为 ProjectResourc
这可能是在黑暗中尝试,但是,我将如何在 PresentationFramework.dll 的以下内部静态方法中设置断点? System.Windows.Documents.TextEditorTyp
我想编写一个快速的 WPF 应用程序,但我发现它在 Windows 7 上看起来与 Windows 10 上完全不同。所有的填充和边距都乱七八糟。我决定添加默认的 PresentationFramew
我最近完成了我使用 Windows 8 开发的第一个 WPF 应用程序。它在我的机器上运行良好。我的一个 friend 也在他的 Windows 8 机器上的 visual studio 中运行了它,
好吧,我有一个奇怪的错误...... 这很好用: private void radioButtonNormalPoint_Checked(object sender, RoutedEventArgs
应用程序无法加载任何 xaml。也不创建空窗口“var abc = new Window1();” 错误消息仍然相同: PresentationFramework.dll 中发生“System.IO.
我在 ASP.NET 应用程序中看到难以理解的编译错误。 Project file must include the .NET Framework assembly 'WindowsBase, Pre
我刚刚从 RedGate 下载了最新版本的 Reflector,但无法反编译某些核心 WPF 程序集中的类,例如 PresentationCore 和 PresentationFramework。 这
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in Presentation
我在 C# 上使用 WPF 作为下面的代码 //My GUI Code here 当我运行应用程序时,它会抛出以下异常
我是一名优秀的程序员,十分优秀!