即使我不改变它,我的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
(或者至少它在我的代码中不可见),并且在此之后我也调用了调试msgBox(我做了{{1}在输入序列时查看存储在那里的数据)。正如您可能已经意识到的那样,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;
}
}
}
}
答案 0 :(得分:0)
您在urlGlobal
内设置lnkDownloadCopy_Loaded()
,因此没有理由与urlGlobalOld
中设置的showCategory()
相同。
String.IsNullOrEmpty(urlGlobal)
是正确的。 urlGlobal
通过以下方式设置:
urlGlobal = globalCopy.Tag as string
globalCopy
是对downloadButtonCopy
;
downloadButtonCopy
是对downloadButton
的引用。
downloadButton.Tag
是通过绑定到我放弃尝试跟踪的某些XML的InnerText
属性来设置的,我甚至怀疑是否在那时进行了评估。
说真的,请使用MVVM。代码是不可能理解到你自己明白不明白的地方。