WPF-动态添加控件-CLR& XAML [英] WPF- adding controls dynamically -CLR & XAML
问题描述
我是WPF和C#的新手,我正在动态添加WPF应用程序中的控件。
我首先用Windows滑块测试我的代码然后它可以工作但是当我尝试添加自定义控件时不起作用。我正在使用Agauge Silverlight 3和WPF的圆形仪表自定义控件 [ ^ ]
我在其他应用程序中使用此控件并且工作正常。要动态添加控件,我将按照本教程 http:// 10rem.net/blog/2010/12/08/dynamically-generating-controls-in-wpf-and-silverlight [ ^ ]
I尝试多个whay
首先我尝试添加这样的仪表:
I am new in WPF and C# and I am adding controls in a WPF app dynamically.
I tested my code with a windows slider first and it works but When I try to add a custom control didn't works. I am using the Agauge Circular gauge custom control for Silverlight 3 and WPF[^]
I used this control in other apps and works perfctly. To add dynamically the controls I am following this tutorial http://10rem.net/blog/2010/12/08/dynamically-generating-controls-in-wpf-and-silverlight[^]
I try in more than one whay
First I try to add the gauge like this:
public partial class DynamicUI : UserControl
{
List<CircularGaugeControl> gauges_list = new List<CircularGaugeControl>();
int flag;
public DynamicUI()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
CreateUIGauges();
}
private void CreateUIGauges()
{
UniformGrid ug = new UniformGrid();
flag = 3;
while (flag != 0)
{
CreateCircularGaude();
flag--;
}
foreach (CircularGaugeControl obj in gauges_list)
{
ug.Children.Add(obj);
}
rootGrid.Children.Add(ug);
LayoutRoot.Children.Add(rootGrid);
}
private void CreateCircularGaude()
{
CircularGaugeControl cg = new CircularGaugeControl();
cg.MaxValue = 500;
cg.MinValue = 0;
cg.Radius = 100;
cg.ScaleLabelRadius = 80;
cg.ScaleStartAngle = 300;
cg.DialText = "RPM";
cg.PointerThickness = 9;
cg.FontSize = 10;
cg.ScaleStartAngle = 140;
cg.ScaleSweepAngle = 200;
gauges_list.Add(cg);
}
}
它不起作用然后我试图在XALM中添加agauge但是来自.cs:
It doesn't work so Then I am trying to add the agauge in XALM but from .cs:
private void CreateCircularGaude()
{
//Option a) XALM ELEMENT
string xaml=
"<Grid Margin='10' " +
"xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +
"xmlns:gauge='clr-namespace:CircularGauge;assembly=CircularGauge'>"+
"<!--DarkSlateGray gauge--> "+
"<gauge:CircularGaugeControl x:Name='myGauge4' Grid.Column='1' Grid.Row'1'"+
" Radius='150'"+
"ScaleRadius='110'"+
"ScaleStartAngle='120'" +
"ResetPointerOnStartUp='True'"+
"ScaleSweepAngle='300'"+
"PointerLength='85'"+
"PointerCapRadius='35'"+
"MinValue='0'"+
"MaxValue='1000'"+
"MajorDivisionsCount='10'"+
"MinorDivisionsCount='5'"+
"CurrentValue='{Binding Score}'"+
"ImageSource='windowslogo.png'"+
"ImageSize='40,50'"+
"RangeIndicatorThickness='0'"+
"RangeIndicatorRadius='0'"+
"ScaleLabelRadius='90'"+
"ScaleLabelSize='40,20'"+
"ScaleLabelFontSize='11'"+
"ScaleLabelForeground='Black'"+
"MajorTickSize='10,3'"+
"MinorTickSize='3,1'"+
"MajorTickColor='DarkGray'"+
"MinorTickColor='DarkGray'"+
"ImageOffset='-50'"+
"GaugeBackgroundColor='DarkSlateGray'"+
"PointerThickness ='12'"+
"OptimalRangeStartValue='300'"+
"OptimalRangeEndValue='700'"+
"DialTextOffset='40'"+
"DialText='Slate Gray'"+
"DialTextColor='DarkSlateGray'>" +
"</gauge:CircularGaugeControl>"+
"</Grid >";
StringReader stringReader = new StringReader(xaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
UIElement tree = (UIElement)XamlReader.Load(xmlReader);
LayoutRoot.Children.Add(tree);
}
应用程序中断并打印此错误消息:
The app breaks and print this error message:
+ $exception {"''' is an unexpected token. The expected token is '='. Line 1, position 302."} System.Exception {System.Windows.Markup.XamlParseException}
但我没有看到任何解析错误。
你知道更好的方法吗?或者为什么会发生这种错误?
but I don't see any parsing error.
Do you know a better way to do this? or why is this error happening?
推荐答案
exception {'''是一个意外的令牌。预期的令牌是'='。第1行,第302位。 System.Exception {System.Windows.Markup.XamlParseException}
exception {"''' is an unexpected token. The expected token is '='. Line 1, position 302."} System.Exception {System.Windows.Markup.XamlParseException}
但我没有看到任何解析错误。
你知道吗?更好的方法吗?或者为什么会发生这种错误?
but I don't see any parsing error.
Do you know a better way to do this? or why is this error happening?
在从这个站点下载的customcontrol中打开circularGaugeDemoWPF项目。
删除来自Window1.XAML的4个控件。
从codebehind中的window1_loaded事件中删除代码。
使用CircularGauge添加;
添加到构造函数:
Open the circularGaugeDemoWPF project in the customcontrol that you downloaded from this site.
Remove the 4 controls from the Window1.XAML.
Remove the code from the window1_loaded event in codebehind.
Add using CircularGauge;
Add to constructor:
CircularGaugeControl cg = new CircularGaugeControl();
cg.MaxValue = 500;
cg.MinValue = 0;
cg.Radius = 100;
cg.ScaleLabelRadius = 80;
cg.ScaleStartAngle = 300;
cg.ScaleLabelFontSize = 12;
cg.DialText = "RPM";
cg.PointerThickness = 9;
cg.FontSize = 10;
cg.ScaleStartAngle = 140;
cg.ScaleSweepAngle = 200;
LayoutRoot.Children.Add(cg);
这不是代码中的副本!
这适用于我的电台。
This is not a copy from your code!
This works on my station.
这篇关于WPF-动态添加控件-CLR& XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!