我遇到了WPF绑定到静态属性的一些问题.在我的应用程序中有一个名为Globals的类,它包含ThisAddIn的静态属性.这个对象允许我访问一个User属性,它基本上是我想在我的Binding中使用的.所以,我的XAML看起来像这样:
<TextBlock Text="{Binding Path=User.Name
, Source={x:Static Member=s:Globals.ThisAddIn}}" />
Run Code Online (Sandbox Code Playgroud)
命名空间s由以下行在顶部声明:
xmlns:s="clr-namespace:ConsoleApplication16.Model"
Run Code Online (Sandbox Code Playgroud)
我发现了很多不同的方法,如何在XAML中引用静态属性,但除了这个之外没有一个工作,这对我来说似乎也是最合乎逻辑的.我发现的一些样本根本没有使用Path或Member.
经过一些测试后我找到了正确的方法,使用"x:Static Member"来定义你想要使用的类和静态属性,而Path在这个对象里面定义了正确的属性,就像普通绑定一样.
尽管此解决方案在没有任何抱怨的情况下进行编译,但会弹出XAMLParseException,直接告诉我StaticExtension值无法解析为枚举,静态字段或静态属性.
我在C#上使用WPF代码如下
<Window
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" mc:Ignorable="d"
xmlns:utility="clr-namespace:DVRClientInterface.Utility"
xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:wpftoolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:customControl="clr-namespace:DVRClientInterface.CustomControls"
x:Class="DVRClientInterface.MainWindow"
WindowStyle="None" ResizeMode="CanMinimize"
AllowsTransparency="True"
WindowState = "Maximized"
WindowStartupLocation="CenterScreen"
StateChanged="Window_StateChanged"
KeyUp="Window_KeyUp"
KeyDown="Window_KeyDown"
Closing="Window_Closing" Loaded="Window_Loaded"
>
<!-- WindowState = "Maximized" -->
<Window.Background>
<ImageBrush ImageSource="Resources\main_background.png"/>
</Window.Background>
<Grid x:Name="MainWindowGrid">
//My GUI Code here
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,它将抛出以下异常
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'DVRClientInterface.MainWindow' that matches the specified binding constraints threw an exception.' Line number '9' and line position '5'.
If there …Run Code Online (Sandbox Code Playgroud) 我一直在尝试部署我的WPF应用程序,我使用安装向导创建了一个安装项目.我添加的唯一项目输出是主要的.在构建并安装程序之后,只要我点击桌面上的exe,我就会弹出一个"我的程序已经停止工作"的弹出窗口,所以我点击Debug the Program并看到
PresentationFramework.dll中发生未处理的"System.Windows.Markup.XamlParseException"类型异常
附加信息:'Set connectionId引发了异常.' 行号"10"和行位置"9".
这个例外并没有指出我要解决什么方向.我的应用中没有任何'connectionId'.
我之前因为我的系统托盘的NotifyIcon而遇到了XAMLParseException,但是通过在我的exe路径中添加图标来解决这个问题.我认为这可能是问题,所以我将图标添加到我的安装项目,以及所有其他项目输出.还是行不通.
我知道这是一个模糊的错误,但任何帮助都会受到赞赏,我的应用程序将无法运行.谢谢!
我有一个都崩溃了,一旦我得到它在没有开发环境installed--如果这是一个傻瓜,我是欢迎的封罐机WPF应用程序,但我我的搜索福未能找到一个等效的问题.我似乎得到了一个XamlParseException,但没有比这更有用了.我需要获得有用的信息.
浏览Windows 7事件日志会给我这个错误日志:
Fault bucket , type 0
Event Name: CLR20r3
Response: Not available
Cab Id: 0
Problem signature:
P1: MyApp.exe
P2: 1.0.0.0
P3: 4b88323d
P4: PresentationFramework
P5: 3.0.0.0
P6: 4a174fbc
P7: 624f
P8: e1
P9: System.Windows.Markup.XamlParse
P10:
Attached files:
C:\Users\Mark\AppData\Local\Temp\WER7DC.tmp.WERInternalMetadata.xml
These files may be available here:
C:\Users\Mark\AppData\Local\Microsoft\Windows\WER\ReportArchive
\AppCrash_generatortestbed_4fa7dff09a9e893eb675f488392571ced4ac8_04ef1100
Analysis symbol:
Rechecking for solution: 0
Report Id: cd55060c-271f-11df-b6ff-001e52eefb8e
Report Status: 1
Run Code Online (Sandbox Code Playgroud)
我检查了那些目录,第一个目录不存在,而第二个目录包含一个只列出加载的dll的wer文件.
我可以在我的测试机器上安装一个开发环境,但是它无法成为一台测试机器而我又回到原点.我没有在安装了开发环境时遇到此错误,因此我对如何获得详细,有用的错误消息感到茫然.
编辑:建立@Alastair Pitts的评论如下,这是我填写异常处理的方式:
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
Exception theException = e.Exception;
string theErrorPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) …Run Code Online (Sandbox Code Playgroud) 当我将xxxx.XAML加载到WPF框架时,它会抛出错误' ', hexadecimal value 0x0C, is an invalid character. Line 1, position 1.如何找到导致问题的原因.当xxxx.XAML在另一个项目的起始页面时,它工作正常,当我在另一个项目中加载xxxx.XAML的意思时,它会抛出一个错误,为什么会发生这种情况?
此外,我还将我的项目从2010年升级到2012年
x:在XAML中重新排列元素后,无法解析引用.
在这里,我提出一个工作代码.只需移动DataGrid元素,使其位于按钮元素之后,并且ButtonMenu中的MenuItem和Button.IsEnabled中的MultiBinding的绑定将被破坏.在Button.IsEnabled中,只有MultiBinding被破坏.它可以用注释块和x替换:参考在单个绑定中起作用.
两者都抛出XamlParseException.
那么什么时候x:引用实际上已经解决了,为什么只有一些绑定在引用元素位于引用它的元素之后才会中断?
这是我的XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xxx="clr-namespace:WpfApplication1"
Title="MainWindow" SizeToContent="WidthAndHeight">
<Window.Resources>
<xxx:BoolToVisibleConverter x:Key="boolToVisibleConv"></xxx:BoolToVisibleConverter>
<xxx:NullToFalseConverter x:Key="nullToFalseConv"></xxx:NullToFalseConverter>
<xxx:NullsOrToFalseConverter x:Key="nullsOrToFalseConv"></xxx:NullsOrToFalseConverter>
<ContextMenu x:Key="MyMenu">
<MenuItem
Header="Menuitem enabled when row selected"
IsEnabled="{Binding
Path=SelectedItem,
Source={x:Reference dataGridElement},
Converter={StaticResource nullToFalseConv}}" />
</ContextMenu>
</Window.Resources>
<StackPanel>
<DataGrid
Name="dataGridElement"
IsReadOnly="True" />
<Button
Content="Button"
ContextMenu="{StaticResource MyMenu}"
Visibility="{Binding
Path=IsReadOnly,
Source={x:Reference dataGridElement},
Converter={StaticResource boolToVisibleConv}}">
<Button.IsEnabled>
<!--<Binding
Path="SelectedItem"
Source="{x:Reference dataGridElement}"
Converter="{StaticResource nullToFalseConv}"/>-->
<MultiBinding
Converter="{StaticResource nullsOrToFalseConv}">
<Binding
Path="SelectedItem"
Source="{x:Reference dataGridElement}"/>
<Binding
Path="SelectedItem"
Source="{x:Reference dataGridElement}"/>
</MultiBinding>
</Button.IsEnabled>
</Button>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是我的代码背后(没有使用):
namespace WpfApplication1
{ …Run Code Online (Sandbox Code Playgroud) 这个XAMLParseException让我疯狂!我在Visual Studio 1020中使用.NET 3.5这是我的xaml:
<Window x:Class="WinformsHost.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Advanced Notepad" Height="350" Width="525" Loaded="Window_Loaded">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="_New" />
<MenuItem Header="_Open..." />
<MenuItem Header="_Save" />
<MenuItem Header="S_ave As..." />
<Separator />
<MenuItem Header="Page Setup" />
<MenuItem Header="Print" />
<Separator />
<MenuItem Header="Exit" />
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Undo" />
<MenuItem Header="Redo" />
<Separator />
<MenuItem Header="Cut" />
<MenuItem Header="Copy" />
<MenuItem Header="Paste" />
<MenuItem Header="Delete" />
<Separator />
<MenuItem Header="Time/Date" />
<Separator />
<MenuItem Header="Preferences" />
</MenuItem>
<MenuItem …Run Code Online (Sandbox Code Playgroud) 我在使用Silverlight Toolkit控件时在Expression Blend中打开我的UserControl时遇到一个奇怪的问题.我的UserControl使用工具包的ListBoxDragDropTarget,如下所示:
<controlsToolkit:ListBoxDragDropTarget mswindows:DragDrop.AllowDrop="True" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<ListBox ItemsSource="{Binding MyItemControls}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</controlsToolkit:ListBoxDragDropTarget>
Run Code Online (Sandbox Code Playgroud)
一切都在运行时按预期工作,在Visual Studio 2008中看起来很好.但是,当我尝试在Blend中打开我的UserControl时,我得到XamlParseException:[Line:0 Position:0],我在设计视图中看不到任何内容.更具体地说,Blend抱怨:
由于System.Windows.Controls.ListBoxDragDropTarget:TargetType不匹配的问题,无法显示元素"ListBoxDragDropTarget".
我的silverlight应用程序引用了2009年11月工具包版本中的System.Windows.Controls.Toolkit,我确保为ListBoxDragDropTarget包含这些命名空间声明:
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:mswindows="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
Run Code Online (Sandbox Code Playgroud)
如果我注释掉ListBoxDragDropTarget控件包装器并且只是离开ListBox,我可以在设计视图中看到一切没有错误.此外,我意识到这种情况正在发生在各种Silverlight Toolkit控件上,因为如果我注释掉ListBoxDragDropTarget并将其替换为
<controlsToolkit:BusyIndicator />
Run Code Online (Sandbox Code Playgroud)
Blend中出现相同的确切错误.甚至更奇怪的是,如果我在混合中启动一个全新的silverlight应用程序,我可以添加这些工具包元素而不会出现任何错误,因此我的项目引用工具包程序集似乎发生了一些愚蠢的事情.
我很确定这与从generic.xaml加载工具包控件的默认样式有关,因为错误与TargetType有关,Blend可能正在尝试加载默认样式.
有没有人遇到过这个问题,或者有什么想法可能是我的问题?
silverlight xaml silverlight-toolkit xamlparseexception expression-blend
我的目标是能够在XAML中编写:
<Grid>
<Rectangle Fill="AliceBlue"
myCore:MyTimePanel.BeginningDate="03/03/2010"
/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
问题: Silverlight XAML无法从字符串中解析DateTime.所以在运行时我有XamlParseException"无法从该字符串创建DateTime".
当我使用一个简单的DependencyProperty时,我只需在getter/setter上添加一个TypeConverterAttribute即可.像这样(来自这里的想法):
[TypeConverter(typeof(DateTimeTypeConverter))]
public DateTime MyDate
{
get { return (DateTime)GetValue(MyDateProperty); }
set { SetValue(MyDateProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
但是附加了 DP,没有getter/setter.如何才能在XAML中编写字符串日期?
谢谢 !
我有一个非常简单的 uwp 应用程序,其中我引用了一个类库,该类库显然也是一个 uwp 项目,并且具有自定义 ContentDialog。当我直接将它作为项目引用时,它工作得很好,并且 ContentDialog 也会打开。但是,当我删除项目并使用其生成的 dll(用于调试模式的 Debug 和用于发布模式的 Release)并引用该 dll 时,我会在该 ContentDialog 的构造函数中收到 xaml Parse 异常。
UWP 客户端应用程序代码
public sealed partial class MainPage : Page
{
private async Task Test()
{
var exitNode = new ExitNodeCode.ExitNode();
await exitNode.AskForPermissionPopup();
}
public MainPage() => InitializeComponent();
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
await Test();
base.OnNavigatedTo(e);
}
}
Run Code Online (Sandbox Code Playgroud)
在Test()方法上抛出异常,但堆栈跟踪(用断点确认)导致该自定义 contentDialog 的构造函数中的 InitializeComponent() 方法。
类库项目中的方法
public async Task AskForPermissionPopup()
{
var dialog = new PermissionDialog();
await dialog.ShowAsync();
}
Run Code Online (Sandbox Code Playgroud)
自定义内容对话框的 …
wpf ×6
xaml ×5
c# ×4
silverlight ×2
.net ×1
binding ×1
c++ ×1
data-binding ×1
deployment ×1
reference ×1
static ×1
uwp ×1