Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# 正在更改的字符串变量_C#_Wpf_String_Code Behind - Fatal编程技术网

C# 正在更改的字符串变量

C# 正在更改的字符串变量,c#,wpf,string,code-behind,C#,Wpf,String,Code Behind,似乎我的字符串正在更改,即使我没有更改它。我会解释: if (globalCopy != null) { urlGlobal = globalCopy.Tag as string; urlGlobalOld = urlGlobal; if (String.IsNullOrEmpty(urlGlobal)) { gta.Content = null; gta.Visibility = V

似乎我的
字符串
正在更改,即使我没有更改它。我会解释:

if (globalCopy != null)
    {
       urlGlobal = globalCopy.Tag as string;
       urlGlobalOld = urlGlobal;
       if (String.IsNullOrEmpty(urlGlobal))
         {
           gta.Content = null;
           gta.Visibility = Visibility.Collapsed;
         }
    }
我在代码中进一步创建了某种调试MessageBox,当我调用它时,它显示以下内容:
globalCopy.tagasstring=“somedata”;urlGlobal=“somedata”;urlgloballd=“”
。问题是,在这个序列之后我没有更改
urlGlobal
(或者至少在代码中我看不到它),并且在这之后我也调用了debug msgBox(我创建了
urlglobald
,以查看在输入序列时存储了哪些数据)。正如您可能意识到的那样,
String.IsNullOrEmpty(urlGlobal)
总是
true
,不应该是。整个代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Xml;
using System.Threading;
using System.Globalization;

namespace AndyLaunchWPF
{
    /// <summary>
    /// Interaction logic for Store.xaml
    /// </summary>
    public partial class Store : Window
    {
        public Store()
        {
            InitializeComponent();
        }
        StackPanel itemElement;
        XmlDataProvider xmldp;
        //XmlDocument xmld;
        string selected = "All";
        ListBox CatList;
        string urlGlobal;
        int i = 0;
        Button globalCopy;
        string urlGlobalOld;
        private void AndyLaunch_Click(object sender, RoutedEventArgs e)
        {
            MainWindow main = new MainWindow();
            App.Current.MainWindow = main;
            this.Close();
            main.Show();
        }
        void Exit(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("The Application is going to be shutted down. Do you want to continue?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }
        private void lnkGoToArticle_Click(object sender, RoutedEventArgs e)
        {
            string url = (sender as Hyperlink).Tag as string;

            if (String.IsNullOrWhiteSpace(url)) return;

            System.Diagnostics.Process.Start(url);
        }

        private void lnkDownload_Click(object sender, RoutedEventArgs e)
        {
            string url = (sender as Button).Tag as string;

        if (String.IsNullOrWhiteSpace(url)) return;
        try { System.Diagnostics.Process.Start(url); }
        catch { MessageBox.Show("bad link"); }
    }
    void showCategory(string category)
    {
        //string se prida k nazvu xml elementu, All=>category=""
        //loadXMLDP("title"+category);
        //if (xmldp.Data.ToString() != "")
        //{
        //for (int i = 0; i == 2; i++)
        //{
        //int i=0;
        //while (i == 5)
        //{
        /*try { urlGlobal = globalCopy.Tag as string; }
        catch { }*/
            TextBlock added = new TextBlock();
            added = addItemTitle(category);
            try
            {

                itemElement.Children.Add(added);
                itemElement.RegisterName(added.Name, added);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fail!\n" + ex.ToString(), ":(");
            }
            Label gta = new Label();
            Hyperlink goToArticle = new Hyperlink();
            goToArticle.Click += new RoutedEventHandler(lnkGoToArticle_Click);
            goToArticle.Inlines.Add(@">>");
            Binding goToArticleBinding = new Binding();
            goToArticleBinding.Path = new PropertyPath("InnerText");
            goToArticleBinding.XPath = "link";
            goToArticle.SetBinding(Hyperlink.TagProperty, goToArticleBinding);
            gta.Content = goToArticle;
            Binding gtaVis = new Binding();
            //updateUrl();
            /*try { urlGlobal = globalCopy.Tag as string; }
            catch { }*/



            //NullToVisibilityConverter ntvc;
            //gtaVis.Converter =

            gta.SetBinding(Label.VisibilityProperty, gtaVis);

            itemElement.Children.Add(gta);
            itemElement.RegisterName(goToArticle.Name, goToArticle);

            Button downloadButton = new Button();
            downloadButton.Name = "lnkDownload";
            downloadButton.Cursor = Cursors.Hand;
            downloadButton.Click += new RoutedEventHandler(lnkDownload_Click);
            Binding downloadButtonBinding = new Binding();


            downloadButtonBinding.XPath = "download" + category;
            downloadButtonBinding.Path = new PropertyPath("InnerText");

            downloadButton.SetBinding(Button.TagProperty, downloadButtonBinding);
            Style downloadButtonStyle = this.FindResource("NoChromeButton") as Style;
            downloadButton.Style = downloadButtonStyle;
            Button downloadButtonCopy = downloadButton;
            downloadButtonCopy.Loaded += new RoutedEventHandler(lnkDownloadCopy_Loaded);
            globalCopy = downloadButtonCopy;
            BitmapImage dbiBitmap = new BitmapImage();
            dbiBitmap.BeginInit();
            dbiBitmap.UriSource = new Uri("pack://application:,,,/AndyLaunchWPF;component/Images/download31.png");
            dbiBitmap.EndInit();
            Image dbi = new Image();
            dbi.Width = 30;
            dbi.Height = 30;
            dbi.Name = "downloadIcon";
            dbi.Source = dbiBitmap;
            downloadButton.Content = dbi;

            itemElement.Children.Add(downloadButton);
            itemElement.RegisterName(downloadButton.Name, downloadButton);
            urlGlobal = globalCopy.Tag as string;
            if (globalCopy != null)
            {
                urlGlobal = globalCopy.Tag as string;
                urlGlobalOld = urlGlobal;
                if (String.IsNullOrEmpty(urlGlobal))
                {
                    gta.Content = null;
                    gta.Visibility = Visibility.Collapsed;
                    //added.Visibility = Visibility.Collapsed;
                }
            }
        //}
            //}
            i++;
        //}
    }
    string updateUrl()
    {
        try { return globalCopy.Tag as string; }
        catch { return urlGlobal; }
    }
    void loadListBox()
    {
        //ItemsList.Items.Clear();

        try { selected = CategoriesList.SelectedItem.ToString(); }
        catch (Exception ex) { MessageBox.Show("Fail!\n" + ex.ToString(), ":("); }
        //if (selected==xmldp

        /*System.Xml.XmlDocument data = new System.Xml.XmlDocument();
        data.Load(@"http://www.andystore.bluefile.cz/?feed=rss2");
        xmldp.Document = data;
        xmldp.XPath = "//item";*/

        //addItemTitle("");
        showCategory("");


        //itemElement.Children.Add(dbi);
        //itemElement.RegisterName(dbi.Name, dbi);
    }
    public void updateListBox()
    {
        try { selected = CategoriesList.SelectedItem.ToString(); }
        catch (Exception ex) { MessageBox.Show("Fail!\n" + ex.ToString(), ":("); }

        itemElement.Children.Clear();
        if (selected == "System.Windows.Controls.ListBoxItem: All")
        {
            itemElement.InvalidateVisual();
            loadListBox();
            //addItemTitle();
            //showCategory("");
        }
        else if (selected == "System.Windows.Controls.ListBoxItem: Games")
        {
            itemElement.InvalidateVisual();
            showCategory("Games");

        }
        else if (selected == "System.Windows.Controls.ListBoxItem: Movies")
        {
            itemElement.InvalidateVisual();
            showCategory("Movies");
        }
        else
        {
            itemElement.InvalidateVisual();
            MessageBox.Show("Fail! Seems that this category doesn't exist", ":(");
        }

    }
    public TextBlock addItemTitle(string category)
    {
        Thickness mrg = new Thickness();
        mrg.Left = 2;
        mrg.Right = 2;
        mrg.Top = 2;
        mrg.Bottom = 2;
        TextBlock itemTitle = new TextBlock();
        itemTitle.Margin = mrg;

        itemTitle.FontSize = 16;
        itemTitle.VerticalAlignment = VerticalAlignment.Center;
        itemTitle.Text = "{Binding XPath=title}";
        itemTitle.FontWeight = FontWeights.Normal;
        itemTitle.Name = "itemTitle";
        Binding itemTitleBinding = new Binding();
        itemTitleBinding.XPath = "title"+category;
        itemTitle.SetBinding(TextBlock.TextProperty, itemTitleBinding);
        return itemTitle;
    }

    void itemElement_Loaded(object sender, RoutedEventArgs e)
    {
        itemElement = sender as StackPanel;
        loadListBox();

    }

    /*private void XmlDataProvider_DataChanged(object sender, EventArgs e)
    {
        xmldp = sender as XmlDataProvider;
    }*/

    private void CategoriesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //urlGlobal = globalCopy.Tag as string;
        CatList = new ListBox();
        //urlGlobal = globalCopy.Tag as string;
        updateListBox();
        //urlGlobal = globalCopy.Tag as string;
    }

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        updateListBox();
    }


    private void MenuItem_Click_1(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(selected);
    }

    void loadXMLDP(string path)
    {
        Thread.Sleep(50);
        xmldp.InitialLoad();
        /*try
        {*/
            xmldp.Source = new Uri("http://www.andystore.bluefile.cz/?feed=rss2");
        /*}
        catch(Exception ex)
        {
            MessageBox.Show("Fail!\n"+ex.ToString(), ":(");
        }
        try
        {*/
            xmldp.XPath = path;
        /*}
        catch (Exception ex)
        {
            MessageBox.Show("Fail!\n" + ex.ToString(), ":(");
        }*/
    }
    public sealed class NullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? Visibility.Hidden : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    private void MenuItem_Click_2(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Copy.Text);
    }

    private void lnkDownloadCopy_Loaded(object sender, RoutedEventArgs e)
    {
        urlGlobal = (sender as Button).Tag as string;

    }

    private void MenuItem_Click_3(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(i.ToString());
    }

    private void MenuItem_Click_4(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(globalCopy.Tag as string+"\nurlGlobal:"+urlGlobal+"\nurlGlobal was:"+urlGlobalOld);
    }
}
public class TextToVisibilityConverter : IValueConverter
{
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is string && targetType == typeof(Visibility))
        {
            if (value.ToString().Equals(string.Empty))
                return Visibility.Hidden;
            else
                return Visibility.Visible;
        }
        else
        {
            return null;
        }
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is Visibility && targetType == typeof(string))
        {
            if ((Visibility)value == Visibility.Visible)
            {
                return "Text";
            }
            else
            {
                return string.Empty;
            }
        }
        else
        {
            return null;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用系统组件模型;
使用System.Xml;
使用系统线程;
利用制度全球化;
名称空间AndyLaunchWPF
{
/// 
///Store.xaml的交互逻辑
/// 
公共部分类存储:窗口
{
公共商店()
{
初始化组件();
}
StackPanel元素;
XmlDataProvider xmldp;
//xmld文档;
字符串selected=“All”;
列表框CatList;
字符串URL全局;
int i=0;
按钮球镜;
字符串urlglobald;
私有void AndyLaunch\u单击(对象发送方,路由目标)
{
MainWindow main=新的MainWindow();
App.Current.MainWindow=main;
这个。关闭();
main.Show();
}
无效退出(对象发送器,路由目标)
{
if(MessageBox.Show(“应用程序即将关闭。是否继续?”,“警告”,MessageBoxButton.YesNo,MessageBoxImage.Warning)=MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
私有void lnkGoToArticle_单击(对象发送者,路由目标e)
{
字符串url=(发件人作为超链接)。标记为字符串;
if(String.IsNullOrWhiteSpace(url))返回;
系统.诊断.过程.启动(url);
}
私有void lnkDownload_单击(对象发送方,路由目标)
{
字符串url=(发送者为按钮)。标记为字符串;
if(String.IsNullOrWhiteSpace(url))返回;
请尝试{System.Diagnostics.Process.Start(url);}
catch{MessageBox.Show(“坏链接”);}
}
无效显示类别(字符串类别)
{
//字符串se prida k nazvu xml elementu,All=>category=“”
//loadXMLDP(“标题”+类别);
//if(xmldp.Data.ToString()!=“”)
//{
//对于(int i=0;i==2;i++)
//{
//int i=0;
//而(i==5)
//{
/*尝试{urlGlobal=globalCopy.tagasstring;}
捕获{}*/
TextBlock added=新的TextBlock();
添加=添加标题(类别);
尝试
{
itemElement.Children.Add(已添加);
itemElement.RegisterName(added.Name,added);
}
捕获(例外情况除外)
{
MessageBox.Show(“失败!\n”+例如ToString(),“:(”);
}
标签gta=新标签();
Hyperlink goToArticle=新建超链接();
goToArticle.Click+=新建路由EventHandler(lnkGoToArticle\u Click);
goToArticle.Inlines.Add(@“>>”);
Binding goToArticleBinding=新绑定();
路径=新属性路径(“InnerText”);
goToArticleBinding.XPath=“link”;
goToArticle.SetBinding(Hyperlink.TagProperty,goToArticleBinding);
gta.Content=goToArticle;
Binding gtaVis=新绑定();
//updateUrl();
/*尝试{urlGlobal=globalCopy.tagasstring;}
捕获{}*/
//NullToVisibilityConverter ntvc;
//gtaVis转换器=
gta.SetBinding(标签可见属性,gtaVis);
itemElement.Children.Add(gta);
itemElement.RegisterName(goToArticle.Name,goToArticle);
按钮下载按钮=新按钮();
downloadButton.Name=“lnkDownload”;
downloadButton.Cursor=Cursors.Hand;
下载按钮。单击+=新建路由EventHandler(lnkDownload_单击);
Binding downloadButtonBinding=新绑定();
downloadButtonBinding.XPath=“下载”+类别;
downloadButtonBinding.Path=新属性路径(“InnerText”);
downloadButton.SetBinding(Button.TagProperty,downloadButtonBinding);
Style downloadButtonStyle=this.FindResource(“NoChromeButton”)作为样式;
Style=downloadbutonstyle;
按钮下载按钮复制=下载按钮;
downloadButtonCopy.Loaded+=新路由EventHandler(lnkDownloadCopy_Loaded);
globalCopy=下载按钮复制;
BitmapImage dbiBitmap=新的BitmapImage();
dbiBitmap.BeginInit();
dbiBitmap.UriSource=新Uri(“pack://application:,,,/AndyLaunchWPF;component/Images/download31.png”);
dbiBitmap.EndInit();
Image dbi=新图像();
dbi.宽度=30;
dbi.高度=30;
dbi.Name=“下载图标”;
dbi.Source=dbiBitmap;
downloadButton.Content=dbi;
itemElement.Children.Add(下载按钮);
itemElement.RegisterName(downloadButton.Name,downloadButton);
urlGlobal=globalCopy.Tag作为字符串;
如果(全局复制!=null)
{
urlGlobal=globalCopy.tagas
urlGlobal = globalCopy.Tag as string