Кажется, что мой string
меняется, даже если я его не меняю. Я объясню:Изменена изменяемая строка
if (globalCopy != null)
{
urlGlobal = globalCopy.Tag as string;
urlGlobalOld = urlGlobal;
if (String.IsNullOrEmpty(urlGlobal))
{
gta.Content = null;
gta.Visibility = Visibility.Collapsed;
}
}
Я сделал какое-то отладка MessageBox дальше в коде, и когда я его называю это показывает следующее: globalCopy.Tag as string="somedata";urlGlobal="somedata";urlGlobalOld=""
. Проблема в том, что я не меняю urlGlobal
после этой последовательности (или, по крайней мере, это не видно в коде для меня), и я вызываю debug msgBox также после этого (я сделал urlGlobalOld
, чтобы посмотреть, какие данные там хранятся при вводе последовательности). Как вы, наверное, поняли, 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;
}
}
}
}
Где вы указываете значение 'Tag'? –
Строка является ссылочным типом, но со специальной обработкой, попробуйте заменить все '.Tag as string' на' .Tag.ToString() ' –
. Я не думаю, что здесь нужна вся стена кода. Подумайте о минимальном рабочем примере, который иллюстрирует проблему. – Fedor