嗯,我的问题是我不确切知道它是如何工作的。在我看来,代码很简单,显然应该可行,但事实并非如此。至少并非总是如此。 (有时它可以工作,但有时却不行。)
在我的应用中,我正在尝试动态添加/删除所有内容:
class Kategoria
{
private CheckBox PriceExists { get; set; }
private TextBox Category { get; set; }
private StackPanel AddFrame { get; set; }
private ComboBox InOrOut { get; set; }
private Button Add { get; set; }
private TextBox Price { get; set; }
private StackPanel MainFrame { get; set; }
public List<Option> Categories { get; set; }
private GridView CategoriesFrame { get; set; }
private List<Grid> CategoriesCollection { get; set; }
public Kategoria(string kategoria, string kwota)
{
}
public Kategoria()
{
TextBlock _title1 = new TextBlock();
TextBlock _title2 = new TextBlock();
InOrOut = new ComboBox();
PriceExists = new CheckBox();
Category = new TextBox();
AddFrame = new StackPanel();
Add = new Button();
Price = new TextBox();
MainFrame = new StackPanel();
Categories = new List<Option>();
CategoriesFrame = new GridView();
CategoriesCollection = new List<Grid>();
InOrOut.Items.Add("Wydatki");
InOrOut.Items.Add("Dochody");
_title1.Text = "Dodaj kategorię";
_title2.Text = "Aktywne kategorię";
PriceExists.Content = "Stała kwota?";
Category.Text = "Nazwa kategorii";
Add.Content = "Dodaj";
Price.Text = "Miesięczna kwota";
_title1.FontSize = 50;
_title2.FontSize = 50;
InOrOut.SelectedIndex = 0;
InOrOut.Height = Category.Height;
InOrOut.Width = 110;
InOrOut.Margin = new Windows.UI.Xaml.Thickness(10);
PriceExists.Margin = new Windows.UI.Xaml.Thickness(10);
Category.Margin = new Windows.UI.Xaml.Thickness(10);
Category.MaxLength = 18;
Price.Margin = new Windows.UI.Xaml.Thickness(10);
Price.MaxLength = 6;
CategoriesFrame.Background = new
Windows.UI.Xaml.Media.SolidColorBrush(Color.FromArgb(21,154,134,23));
CategoriesFrame.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
CategoriesFrame.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
CategoriesFrame.SelectionMode = ListViewSelectionMode.Multiple;
CategoriesFrame.Height = 380;
CategoriesFrame.Width = 900;
PriceExists.Checked+=Kw_Checked;
PriceExists.Unchecked+=Kw_Unchecked;
Category.GotFocus += Kat_Tapped;
Price.GotFocus += Kwota_Tapped;
Add.Click += Add_Click;
AddFrame.Orientation = Orientation.Horizontal;
MainFrame.Width = 1000;
CategoriesFrame.Margin = new Windows.UI.Xaml.Thickness(0, 0, 0, 10);
MainFrame.Orientation = Orientation.Vertical;
MainFrame.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
MainFrame.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
AddFrame.Children.Add(PriceExists);
AddFrame.Children.Add(InOrOut);
AddFrame.Children.Add(Category);
AddFrame.Children.Add(Add);
MainFrame.Children.Add(_title1);
MainFrame.Children.Add(AddFrame);
MainFrame.Children.Add(_title2);
MainFrame.Children.Add(CategoriesFrame);
FillGrids();
}
private void AddGrid()
{
Grid Div = new Grid();
CheckBox new_Cat = new CheckBox();
if (PriceExists.IsChecked==true)
new_Cat.Content = "["+InOrOut.SelectedItem.ToString()+"] "+Category.Text+"
"+Price.Text+"zł";
else
new_Cat.Content = "[" + InOrOut.SelectedItem.ToString() + "] " + Category.Text + "
" + "0" + "zł";
Div.MinWidth = 270;
Div.RightTapped += Div_RightTapped;
Div.Children.Add(new_Cat);
CategoriesCollection.Add(Div);
CategoriesFrame.Items.Add(Div);
PriceExists.IsChecked = false;
Category.Text = "Nazwa kategorii";
}
private void Div_RightTapped(object sender,
Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
{
//problematyczny kod.
if (CategoriesFrame.SelectedItems.Count == 1 &&
CategoriesFrame.SelectedItems[0].Equals(sender) )
{
MainFrame.Children.Remove(MainFrame.Children.OfType<Button>().First());
}
else
{
if (MainFrame.Children.OfType<Button>().SingleOrDefault() == null)
{
Button remove = new Button();
remove.Content = "Usuń zaznaczone kategorię";
remove.Click += Remove_Click;
MainFrame.Children.Add(remove);
}
}
}
private async void Remove_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (CategoriesFrame.SelectedItems.Count > 0)
{
Categories.RemoveAt(CategoriesCollection.IndexOf
((Grid)CategoriesFrame.SelectedItems[0]));
CategoriesCollection.Remove((Grid)CategoriesFrame.SelectedItems[0]);
CategoriesFrame.Items.Remove(CategoriesFrame.SelectedItems[0]);
Remove_Click(this, null);
await Serialize();
}
else
{
MainFrame.Children.Remove(MainFrame.Children.OfType<Button>().SingleOrDefault());
}
}
private async void FillGrids()
{
await Deserialize();
foreach (Option Opcja in Categories)
{
CheckBox Cat_active = new CheckBox();
Grid Div = new Grid();
Div.MinWidth = 100;
Cat_active.Content = "[" + Opcja.InOut + "] " +
Opcja.Title + " " + Opcja.Value + "zł";
Div.Children.Add(Cat_active);
Div.RightTapped+=Div_RightTapped;
CategoriesCollection.Add(Div);
}
foreach (Grid Grd in CategoriesCollection)
{
CategoriesFrame.Items.Add(Grd);
}
}
private async void Add_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (PriceExists.IsChecked == true)
Categories.Add(new
Option(InOrOut.SelectedItem.ToString(),Category.Text,Price.Text));
else
Categories.Add(new Option(InOrOut.SelectedItem.ToString(),Category.Text, "0"));
await Serialize();
AddGrid();
}
private async Task Serialize()
{
using (var stream = await
ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("Opcje.txt",
CreationCollisionOption.ReplaceExisting))
{
XmlSerializer Serializer = new XmlSerializer(typeof(List<Option>));
using (XmlWriter xml = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true }))
{
Serializer.Serialize(xml, Categories);
xml.Dispose();
}
}
}
private async Task Deserialize()
{
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("Opcje.txt"))
{
XmlSerializer Deserializer = new XmlSerializer(typeof(List<Option>));
using (XmlReader xml = XmlReader.Create(stream))
{
Categories = (List<Option>)Deserializer.Deserialize(xml);
xml.Dispose();
}
}
}
private void Kwota_Tapped(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (Price.Text == "Miesięczna kwota")
Price.Text = "";
}
private void Kat_Tapped(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (Category.Text == "Nazwa kategorii")
Category.Text = "";
}
private void Kw_Unchecked(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
AddFrame.Children.Remove(Price);
}
private void Kw_Checked(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
AddFrame.Children.Remove(Add);
AddFrame.Children.Add(Price);
AddFrame.Children.Add(Add);
}
public StackPanel GetHndlr()
{
return MainFrame;
}
}
问题出在这个方法中:
private void Div_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
{
//problematic code
if (CategoriesFrame.SelectedItems.Count == 1 && CategoriesFrame.SelectedItems[0].Equals(sender) )
{
MainFrame.Children.Remove(MainFrame.Children.OfType<Button>().First());
}
else
{
if (MainFrame.Children.OfType<Button>().SingleOrDefault() == null)
{
Button remove = new Button();
remove.Content = "Usuń zaznaczone kategorię";
remove.Click += Remove_Click;
MainFrame.Children.Add(remove);
}
}
}
理论上它应该运作良好而且(有时)。时不时,它没有做它应该做的事情。简单地说,当取消选择GridView中的最后一项时,该按钮不会从MainFram(Stackpanel)中删除,有时当我尝试选择一个项目时,按钮不会添加到MainFrame中。
我想知道为什么会这么随意发生。