XMLDataProvider不会返回具有联合的XPath查询的结果。请在密码后查看我在瓶装的问题说明。
在WPF XMLDataProvider中,我使用下面的somestrings.xml,
<?xml version="1.0" encoding="utf-8" ?>
<MyRoot>
<App1>
<Common>
<ApplicationName>Online Games</ApplicationName>
</Common>
<Screen1>
<SelectGameshButtonName>Select Games</SelectGameshButtonName>
</Screen1>
<Screen2>
<FinishButtonName>Finish Purchase</FinishButtonName>
</Screen2>
</App1>
</MyRoot>XAML代码是
<Window x:Class="WPF_XML.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="SomeStrings" Source="pack://siteoforigin:,,,/somestrings.xml" XPath="MyRoot/App1/Common|MyRoot/App1/Screen1"/>
</Window.Resources>
<Grid >
<Label Content="{Binding Source={StaticResource SomeStrings}, XPath=ApplicationName}" Height="28" HorizontalAlignment="Left" Margin="38,82,0,0" Name="lblName" VerticalAlignment="Top" Width="107" />
<Button Content="{Binding Source={StaticResource SomeStrings}, FallbackValue=oops, XPath=SelectGameshButtonName}" Height="24" HorizontalAlignment="Left" Margin="169,82,0,0" Name="btnSelect" VerticalAlignment="Top" Width="104" />
</Grid>
</Window>使用XMLDataProvider,我试图从xml文件中将显示文本分配给控件。下面是我的观察,
XPath="MyRoot/App1/Common" then label gets value.
XPath="MyRoot/App1/Screen1" then button gets value由于我希望这两个控件都能在单个查询中获得值,所以请使用XPath的联合,
XPath="MyRoot/App1/Common|MyRoot/App1/Screen1"但我看到只有标签在更新。
为什么XMLDataProvider未能将按钮名称返回到绑定。
这是XMLDataProvider的问题还是我遗漏了什么?
谢谢
编辑
虽然将XPath="MyRoot/App1“设置为XMLDataProvider,并将控件内容绑定设置如下,
XPath="Common/ApplicationName|Screen1/ApplicationName"
XPath="Common/SelectGameshButtonName|Screen1/SelectGameshButtonName" 在功能上工作!但我不想用这种方法,
从性能的角度来看。它将在XMLDataProvider中加载所有屏幕xml节点,而不仅仅是普通的screen1。
在屏幕上工作的开发人员应该只对控件使用节点名,而不使用任何前缀规范。他们不应该关心字符串的位置。因为随着时间的推移,字符串可能会被移动到公共的位置。
发布于 2014-06-01 11:28:06
您的XmlDataProvider返回多个项,但是由于您使用UI控件来显示单个项(Label和Button,而不是ListBox、ItemsControl等),所以只能获得两个UI控件( <Common>...</Common>元素)显示的第一个项。
问题不在于使用XPath联合。即使不使用union,如果XML中有多个名称相同的元素,则使用您的方法只会显示第一个元素。
要解决这个问题,可以使用以下XmlDataProvider声明XPath:
<XmlDataProvider XPath="MyRoot/App1" x:Key="SomeStrings" Source="pack://siteoforigin:,,,/somestrings.xml" />然后为您的Label和Button使用下面的Label:
<Label Content="{Binding Source={StaticResource SomeStrings}, XPath=Common/ApplicationName}" Height="28" HorizontalAlignment="Left" Margin="38,82,0,0" Name="lblName" VerticalAlignment="Top" Width="107" />
<Button Content="{Binding Source={StaticResource SomeStrings}, FallbackValue=oops, XPath=Screen1/SelectGameshButtonName}" Height="24" HorizontalAlignment="Left" Margin="169,82,0,0" Name="btnSelect" VerticalAlignment="Top" Width="104" />https://stackoverflow.com/questions/23979111
复制相似问题