UWP C#菜单无法在第一次单击时正确显示项目

UWP C#菜单无法在第一次单击时正确显示项目,c#,uwp,windows-iot-core-10,C#,Uwp,Windows Iot Core 10,我在json中添加代码按钮时遇到问题,第一次尝试单击add按钮时,菜单将不会有任何响应,但第二次单击尝试后,它将正常工作。 我做错什么了吗?谢谢 private async void AddButton_Click(object sender, RoutedEventArgs e) { List<ClientList> clientLists; var jsonSerializer = new DataContractJsonSerializer(typeof(Lis

我在
json
中添加代码按钮时遇到问题,第一次尝试单击
add按钮时,
菜单将不会有任何响应,但第二次单击尝试后,它将正常工作。
我做错什么了吗?谢谢

private async void AddButton_Click(object sender, RoutedEventArgs e)
{
    List<ClientList> clientLists;
    var jsonSerializer = new DataContractJsonSerializer(typeof(List<ClientList>));


    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(CLIENTSLIST);

    clientLists = (List<ClientList>)jsonSerializer.ReadObject(myStream);
    var menuFlyout = new MenuFlyout();

    int isEmpty = myGrid.Children.Count;
    if (isEmpty == 0)
    {
        foreach (var device in clientLists)
        {
            var menuFlyoutItem = new MenuFlyoutItem() { Name = device.clientname, Text = device.clientname };
            menuFlyoutItem.Tag = device.clientaddress;
            menuFlyoutItem.Click += AddMenuFlyoutItem_Click;
            menuFlyout.Items.Add(menuFlyoutItem);
        }
    }else
    {
        foreach (var device in clientLists)
        {
            bool toAddButton = true;
            foreach (Button btn in myGrid.Children.OfType<Button>())
            {
                if (btn.Content.ToString() == device.clientname)
                {
                    toAddButton = false;
                }
            }
            if (toAddButton)
            {
                var menuFlyoutItem = new MenuFlyoutItem() { Name = device.clientname, Text = device.clientname };
                menuFlyoutItem.Tag = device.clientaddress;
                menuFlyoutItem.Click += AddMenuFlyoutItem_Click;
                menuFlyout.Items.Add(menuFlyoutItem);
            }
        }
    }
    AddButton.Flyout = menuFlyout;
}
private async void AddButton\u单击(对象发送器,路由目标)
{
列出客户名单;
var jsonSerializer=新数据契约jsonSerializer(typeof(List));
var myStream=wait ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(CLIENTSLIST);
clientLists=(List)jsonSerializer.ReadObject(myStream);
var menuFlyout=新的menuFlyout();
int isEmpty=myGrid.Children.Count;
如果(isEmpty==0)
{
foreach(客户端列表中的var设备)
{
var menuFlyoutItem=new menuFlyoutItem(){Name=device.clientname,Text=device.clientname};
menuFlyoutItem.Tag=device.clientaddress;
menuFlyoutItem.Click+=添加menuFlyoutItem\u单击;
menuFlyout.Items.Add(menuFlyoutItem);
}
}否则
{
foreach(客户端列表中的var设备)
{
bool toAddButton=真;
foreach(myGrid.Children.OfType()中的按钮btn)
{
if(btn.Content.ToString()==device.clientname)
{
toAddButton=false;
}
}
如果(toAddButton)
{
var menuFlyoutItem=new menuFlyoutItem(){Name=device.clientname,Text=device.clientname};
menuFlyoutItem.Tag=device.clientaddress;
menuFlyoutItem.Click+=添加menuFlyoutItem\u单击;
menuFlyout.Items.Add(menuFlyoutItem);
}
}
}
AddButton.Flyout=菜单输出;
}

问题在于您在此处异步加载数据:

var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(CLIENTSLIST);
发生这种情况时,UI将继续执行
单击
事件,因此按钮将被单击(并且
弹出按钮
第一次为
),并且
弹出按钮
将永远不会显示。您应该在这之前加载
弹出按钮
——无论是在页面加载时还是在数据源更改时,这样当用户单击时,弹出按钮就已经存在了。如果需要异步操作才能完成,那么在
单击中加载就太晚了

或者,也可以在开始处设置弹出按钮:

private async void AddButton_Click(object sender, RoutedEventArgs e)
{
    var menuFlyout = new MenuFlyout();
    AddButton.Flyout = menuFlyout;
    List<ClientList> clientLists;
    var jsonSerializer = new DataContractJsonSerializer(typeof(List<ClientList>));


    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(CLIENTSLIST);

    clientLists = (List<ClientList>)jsonSerializer.ReadObject(myStream);

    int isEmpty = myGrid.Children.Count;
    if (isEmpty == 0)
    {
        foreach (var device in clientLists)
        {
            var menuFlyoutItem = new MenuFlyoutItem() { Name = device.clientname, Text = device.clientname };
            menuFlyoutItem.Tag = device.clientaddress;
            menuFlyoutItem.Click += AddMenuFlyoutItem_Click;
            menuFlyout.Items.Add(menuFlyoutItem);
        }
    }else
    {
        foreach (var device in clientLists)
        {
            bool toAddButton = true;
            foreach (Button btn in myGrid.Children.OfType<Button>())
            {
                if (btn.Content.ToString() == device.clientname)
                {
                    toAddButton = false;
                }
            }
            if (toAddButton)
            {
                var menuFlyoutItem = new MenuFlyoutItem() { Name = device.clientname, Text = device.clientname };
                menuFlyoutItem.Tag = device.clientaddress;
                menuFlyoutItem.Click += AddMenuFlyoutItem_Click;
                menuFlyout.Items.Add(menuFlyoutItem);
            }
        }
    }
}
private async void AddButton\u单击(对象发送器,路由目标)
{
var menuFlyout=新的menuFlyout();
AddButton.Flyout=菜单输出;
列出客户名单;
var jsonSerializer=新数据契约jsonSerializer(typeof(List));
var myStream=wait ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(CLIENTSLIST);
clientLists=(List)jsonSerializer.ReadObject(myStream);
int isEmpty=myGrid.Children.Count;
如果(isEmpty==0)
{
foreach(客户端列表中的var设备)
{
var menuFlyoutItem=new menuFlyoutItem(){Name=device.clientname,Text=device.clientname};
menuFlyoutItem.Tag=device.clientaddress;
menuFlyoutItem.Click+=添加menuFlyoutItem\u单击;
menuFlyout.Items.Add(menuFlyoutItem);
}
}否则
{
foreach(客户端列表中的var设备)
{
bool toAddButton=真;
foreach(myGrid.Children.OfType()中的按钮btn)
{
if(btn.Content.ToString()==device.clientname)
{
toAddButton=false;
}
}
如果(toAddButton)
{
var menuFlyoutItem=new menuFlyoutItem(){Name=device.clientname,Text=device.clientname};
menuFlyoutItem.Tag=device.clientaddress;
menuFlyoutItem.Click+=添加menuFlyoutItem\u单击;
menuFlyout.Items.Add(menuFlyoutItem);
}
}
}
}

这样,弹出按钮将出现,但在异步加载完成并实际添加项目之前,弹出按钮将为空。在这里,您只是在读取一个文件,因此它几乎不应该被注意到。虽然不像预装弹出按钮那样干净,但它也应该完成工作。

您能给代码添加一些注释吗?现在还不清楚if/else语句在检查什么。嗨@MartinZikmund谢谢。因为我的
菜单的内容随着
clientlist
的更改而更改。是否有任何建议可以将异步操作放在当前上下文中的何处?或者,唯一的方法就是像您在
clientlist
更改时提到的那样,尝试第二个建议-例如,移动AddButton.Flyout=menuFlyout;到topHi@MartinZikmund。。第二个建议似乎不起作用。。我将这些行移动到加载的
页面
,它就工作了
var jsonSerializer=newdatacontractjsonserializer(typeof(List));var myStream=wait ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(CLIENTSLIST);clientLists=(List)jsonSerializer.ReadObject(myStream)