Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight和Csharp关于图像的两难选择。if语句是否与此相关?_C#_Silverlight - Fatal编程技术网

C# Silverlight和Csharp关于图像的两难选择。if语句是否与此相关?

C# Silverlight和Csharp关于图像的两难选择。if语句是否与此相关?,c#,silverlight,C#,Silverlight,我有一个这样的查询,它抛出了一个错误,表示imgSortUp在当前上下文中不存在。这是虫子吗?有什么想法吗?在我将图像声明放入if语句之前,它工作得很好。任何帮助都将不胜感激 ContentPresenter cp = (sender as Grid).GetVisualDescendants().OfType<ContentPresenter>().SingleOrDefault(); if (cp.Content.ToString() == "Work Order

我有一个这样的查询,它抛出了一个错误,表示imgSortUp在当前上下文中不存在。这是虫子吗?有什么想法吗?在我将图像声明放入if语句之前,它工作得很好。任何帮助都将不胜感激

ContentPresenter cp = (sender as Grid).GetVisualDescendants().OfType<ContentPresenter>().SingleOrDefault();
        if (cp.Content.ToString() == "Work Order #" || cp.Content.ToString() == "Status")
        {
            Image imgSortUp = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault();
            Image imgSortDown = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault();
        }
        else
            return;

        if (clearOldSortIcons != null)
        {
            Image oldSortIconUp = (clearOldSortIcons as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault();
            Image oldSortIconDown = (clearOldSortIcons as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault();
            oldSortIconUp.Visibility = System.Windows.Visibility.Collapsed;
            oldSortIconDown.Visibility = System.Windows.Visibility.Collapsed;
        }

    start:

        if (!string.IsNullOrEmpty(cp.Content.ToString()) && (string.IsNullOrEmpty(sortCheck) || sortCheck == cp.Content.ToString()))
        {
            if (hasSorted == false)
            {
                switch (cp.Content.ToString())
                {
                    case "Work Order #":
                        imgSortUp.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "Status":
                        imgSortUp.Visibility = System.Windows.Visibility.Visible;
                        break;
                }
                hasSorted = true;
            }
            else
            {
                switch (cp.Content.ToString())
                {
                    case "Title":
                        imgSortDown.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "Status":
                        imgSortDown.Visibility = System.Windows.Visibility.Visible;
                        break;
                }
                hasSorted = false;
            }
        }
        else
        {
            sortCheck = cp.Content.ToString();
            hasSorted = false;
            goto start;
        }
        sortCheck = cp.Content.ToString();
        clearOldSortIcons = sender;
        SortItems(cp.Content.ToString(), hasSorted);
        dpVideos.Source = grdVideos.ItemsSource;
ContentPresenter cp=(发送方作为网格).getVisualDescendats().OfType().SingleOrDefault();
如果(cp.Content.ToString()=“工单”| | | cp.Content.ToString()=“状态”)
{
Image imgSortUp=(发送方作为网格).getVisualDescendats().OfType().Where(i=>i.Name==“SortIconUp”).SingleOrDefault();
Image imgSortDown=(发送方作为网格).getVisualDescendats().OfType().Where(i=>i.Name==“SortIconDown”).SingleOrDefault();
}
其他的
返回;
if(clearOldSortIcons!=null)
{
Image oldSortIconUp=(clearOldSortIcons作为网格)。getVisualDescendats().OfType()。其中(i=>i.Name==“SortIconUp”).SingleOrDefault();
Image oldSortIconDown=(clearOldSortIcons作为网格)。getVisualDescendats().OfType()。其中(i=>i.Name==“SortIconDown”).SingleOrDefault();
oldSortIconUp.Visibility=System.Windows.Visibility.Collapsed;
oldSortIconDown.Visibility=System.Windows.Visibility.Collapsed;
}
开始:
如果(!string.IsNullOrEmpty(cp.Content.ToString())&&(string.IsNullOrEmpty(sortCheck)| | sortCheck==cp.Content.ToString())
{
if(hasorted==false)
{
开关(cp.Content.ToString())
{
案例“工作指令”:
imgSortUp.Visibility=System.Windows.Visibility.Visible;
打破
案例“状态”:
imgSortUp.Visibility=System.Windows.Visibility.Visible;
打破
}
hasSorted=真;
}
其他的
{
开关(cp.Content.ToString())
{
案例“标题”:
imgSortDown.Visibility=System.Windows.Visibility.Visible;
打破
案例“状态”:
imgSortDown.Visibility=System.Windows.Visibility.Visible;
打破
}
hasSorted=假;
}
}
其他的
{
sortCheck=cp.Content.ToString();
hasSorted=假;
转到开始;
}
sortCheck=cp.Content.ToString();
clearOldSortIcons=发送方;
SortItems(cp.Content.ToString(),hasSorted);
dpVideos.Source=grdVideos.ItemsSource;

nooooooo,不是goto

是的,当您声明一个作用域{}时,通常无法从外部访问其中声明的任何变量

要绕过它,您只需声明
imageimgsortup在if语句外,然后在if语句内赋值。只需注意,在赋值之前,imgSortUp将等于null。在初始化之前调用它将导致运行时错误

图像imgSortUp;
图像imgSortDown;
如果(cp.Content.ToString()=“工单”| | | cp.Content.ToString()=“状态”)
{
imgSortUp=(作为网格的发送方)。getVisualDescendats().OfType()。其中(i=>i.Name==“SortIconUp”).SingleOrDefault();
imgSortDown=(发送方作为网格).getVisualDescendats().OfType().Where(i=>i.Name==“SortIconDown”).SingleOrDefault();
}
其他的
返回;

这是因为您在自己的范围内声明图像,并在以后使用它们:

    // declare this image within the outer scope
    Image imgSortDown = null;

    if (cp.Content.ToString() == "Work Order #" || cp.Content.ToString() == "Status") 
    { 
        // declare image within the inner scope
        Image imgSortUp;

        imgSortUp = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault(); 
        imgSortDown = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault(); 
    } 

    // do more work

    imgSortUp.Visibility = true; // This wont work since the image is out of scope.
    imgSortDown.Visibility = true; // This will work since the images is still in scope.
//在外部范围内声明此映像
图像imgSortDown=null;
如果(cp.Content.ToString()=“工单”| | | cp.Content.ToString()=“状态”)
{ 
//在内部范围内声明映像
图像增强;
imgSortUp=(作为网格的发送方)。getVisualDescendats().OfType()。其中(i=>i.Name==“SortIconUp”).SingleOrDefault();
imgSortDown=(发送方作为网格).getVisualDescendats().OfType().Where(i=>i.Name==“SortIconDown”).SingleOrDefault();
} 
//多做点工作
imgSortUp.Visibility=true;//这不起作用,因为图像超出范围。
imgSortDown.Visibility=true;//这将起作用,因为图像仍在范围内。

图像imgSortUp
此声明使其成为
if
声明的局部范围,因此在ITI之外不可用。如果我可以标记为回答您的两个问题,我将清楚地这样做。:)谢谢
    // declare this image within the outer scope
    Image imgSortDown = null;

    if (cp.Content.ToString() == "Work Order #" || cp.Content.ToString() == "Status") 
    { 
        // declare image within the inner scope
        Image imgSortUp;

        imgSortUp = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault(); 
        imgSortDown = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault(); 
    } 

    // do more work

    imgSortUp.Visibility = true; // This wont work since the image is out of scope.
    imgSortDown.Visibility = true; // This will work since the images is still in scope.