[英]UWP C# System.InvalidCastException
I have previously asked on a post regarding how to dynamically add buttons . 我以前曾在帖子中询问过如何动态添加按钮 。 Now I have added additional
image
beside the button
to indicate the connection status of the client(s) . 现在,我在
button
旁边添加了其他image
,以指示客户端的连接状态 。 I have 2 buttons
one to add and another to delete the button
and image
. 我有2个
buttons
一个要添加 ,另一个要删除 button
和image
。 Once clicked there will be a menuflyout
to select the respective button to be deleted. 单击后,将
menuflyout
以选择要删除的相应按钮。 My problem is I manage to add the status image
but when comes to clicking at the delete button
, the menuflyout
runs into exception
which says System.InvalidCastException
. 我的问题是我设法添加状态
image
但是当单击删除 button
, menuflyout
遇到exception
,该exception
表示System.InvalidCastException
。
My code as follow; 我的代码如下; please help.
请帮忙。 Thanks.
谢谢。
public Uri connectedUri = new Uri("ms-appx:///Assets/icon-connected.png");
public Uri disconnectedUri = new Uri("ms-appx:///Assets/icon-disconnected.png");
private void AddMenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
var item = sender as MenuFlyoutItem;
var deviceName = item.Text;
var deviceIP = item.Tag;
Button b = new Button();
b.Height = 30;
b.Width = 100;
b.VerticalAlignment = VerticalAlignment.Top;
b.HorizontalAlignment = HorizontalAlignment.Left;
b.Margin = new Thickness(20, 20, 0, 0);
b.Content = deviceName; // "Button " + buttonCounter;
b.Tag = deviceIP;
b.Click += AddMenuButton_Click;
Image stsImg = new Image();
BitmapImage bitmapImage = new BitmapImage();
//Uri connectedUri = new Uri("ms-appx:///Assets/icon-connected");
//Uri disconnectedUri = new Uri("ms-appx:///Assets/icon-disconnected");
bitmapImage.UriSource = connectedUri;
stsImg.Source = bitmapImage;
stsImg.Width = 20;
stsImg.Height = 20;
stsImg.VerticalAlignment = VerticalAlignment.Top;
stsImg.HorizontalAlignment = HorizontalAlignment.Left;
stsImg.Margin = new Thickness(130, 20, 0, 0);
stsImg.Name = deviceName;
stsImg.Tag = deviceIP;
//Calculate the place of the button
int column = (int)(buttonCounter / 4);
int row = buttonCounter % 4;
//Check if you need to add a columns
if (row == 0)
{
ColumnDefinition col = new ColumnDefinition();
col.Width = new GridLength(column, GridUnitType.Auto);
myGrid.ColumnDefinitions.Add(col);
}
//Add the button
myGrid.Children.Add(b);
Grid.SetColumn(b, column);
Grid.SetRow(b, row);
myGrid.Children.Add(stsImg);
Grid.SetColumn(stsImg, column);
Grid.SetRow(stsImg, row);
buttonCounter++;
}
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
var menuFlyout = new MenuFlyout();
try
{
foreach (Button btn in myGrid.Children)
{
var row = Grid.GetRow(btn);
var col = Grid.GetColumn(btn);
//ButtonRow.Add(btn.Content.ToString(), row.ToString());
//ButtonCol.Add(btn.Content.ToString(), col.ToString());
var menuFlyoutItem = new MenuFlyoutItem() { Name = btn.Tag.ToString(), Text = btn.Content.ToString() };
menuFlyoutItem.Tag = btn.Content.ToString();
menuFlyoutItem.Click += RemoveMenuFlyoutItem_Click;
menuFlyout.Items.Add(menuFlyoutItem);
}
RemoveButton.Flyout = menuFlyout;
}
catch (Exception)
{
}
}
The problem is the foreach
: 问题是
foreach
:
foreach (Button btn in myGrid.Children)
When written like this, it tries to cast all children of the Grid
to Button
. 当这样编写时,它将尝试将
Grid
所有子代转换为Button
。 This is not possible, because some of the children are not buttons. 这是不可能的,因为某些孩子不是按钮。 You can however write the loop like this:
但是,您可以这样编写循环:
foreach (Button btn in myGrid.Children.OfType<Button>())
The OfType<T>
LINQ extension method will filter the input enumerable only to those items which are of a given type. OfType<T>
LINQ扩展方法将仅将可枚举的输入筛选为给定类型的那些项。
Are all the children of myGrid
, of Type Button
? myGrid
所有子级都myGrid
Button
吗?
foreach (Button btn in myGrid.Children)
No, because myGrid.Children.Add(stsImg);
不,因为
myGrid.Children.Add(stsImg);
When you try to get stsImg
as a Button
, you have that exception, because stsImg
is not of type Button
. 当您尝试将
stsImg
作为Button
,您会遇到该异常,因为stsImg
不是Button
类型。
You need to filter only the buttons. 您只需要过滤按钮。
foreach (Button btn in myGrid.Children.OfType<Button>()){
// do stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.