Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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#_Windows 8_Microsoft Metro - Fatal编程技术网

C# 网格事件并不总是发生

C# 网格事件并不总是发生,c#,windows-8,microsoft-metro,C#,Windows 8,Microsoft Metro,我的问题是我不知道它是怎么工作的。在我看来,代码是简单的,它显然应该工作,但它没有。至少不总是这样。(有时有效,但有时无效) 在我的应用程序中,我尝试动态添加/删除所有内容: class Kategoria { private CheckBox PriceExists { get; set; } private TextBox Category { get; set; } private StackPanel AddFrame { get; set; } pri

我的问题是我不知道它是怎么工作的。在我看来,代码是简单的,它显然应该工作,但它没有。至少不总是这样。(有时有效,但有时无效)

在我的应用程序中,我尝试动态添加/删除所有内容:

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;
    }
}
class-Kategoria
{
private复选框PriceExists{get;set;}
专用文本框类别{get;set;}
私有StackPanel AddFrame{get;set;}
私有组合框InOrOut{get;set;}
专用按钮添加{get;set;}
私有文本框价格{get;set;}
专用StackPanel大型机{get;set;}
公共列表类别{get;set;}
私有GridView分类框架{get;set;}
私有列表分类集合{get;set;}
公共卡蒂戈里亚(弦乐卡蒂戈里亚、弦乐郭塔)
{
}
公共卡蒂戈里亚()
{
TextBlock _title1=新的TextBlock();
TextBlock _title2=新的TextBlock();
InOrOut=新组合框();
PriceExists=新复选框();
类别=新文本框();
AddFrame=新的StackPanel();
添加=新按钮();
价格=新文本框();
大型机=新StackPanel();
类别=新列表();
CategoriesFrame=新建GridView();
CategoriesCollection=新列表();
InOrOut.Items.Add(“Wydatki”);
InOut.Items.添加(“正文”);
_标题1.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=类别.Height;
InOrOut.Width=110;
InOrOut.Margin=newwindows.UI.Xaml.Thickness(10);
PriceExists.Margin=newwindows.UI.Xaml.Thickness(10);
Category.Margin=newwindows.UI.Xaml.Thickness(10);
Category.MaxLength=18;
Price.Margin=新Windows.UI.Xaml.Thickness(10);
Price.MaxLength=6;
CategoriesFrame.Background=new
SolidColorBrush(Color.FromArgb(21154134,23));
CategoriesFrame.HorizontalAlignment=Windows.UI.Xaml.HorizontalAlignment.Left;
CategoriesFrame.VerticalAlignment=Windows.UI.Xaml.VerticalAlignment.Top;
CategoriesFrame.SelectionMode=ListViewSelectionMode.Multiple;
分类框架。高度=380;
分类框架。宽度=900;
存在价格。已检查+=已检查的功率;
价格存在。未选中+=Kw\u未选中;
Category.GotFocus+=Kat_;
Price.GotFocus+=Kwota_;
添加.单击+=添加\单击;
AddFrame.Orientation=Orientation.Horizontal;
主机,宽度=1000;
CategoriesFrame.Margin=newwindows.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(类别);
AddFrame.Children.Add(添加);
MainFrame.Children.Add(_title1);
MainFrame.Children.Add(AddFrame);
大型机.Children.Add(_title2);
MainFrame.Children.Add(CategoriesFrame);
填充网格();
}
私有void AddGrid()
{
Grid Div=新网格();
复选框new_Cat=新复选框();
if(PriceExists.IsChecked==true)
new_Cat.Content=“[”+InOrOut.SelectedItem.ToString()+”]“+Category.Text+”
“+Price.Text+zł”;
其他的
new_Cat.Content=“[”+InOrOut.SelectedItem.ToString()+”]“+Category.Text+”
“+”0“+”zł”;
Div.MinWidth=270;
Div.rightstapped+=Div_rightstapped;
添加子类(新的_类);
分类收集。添加(Div);
CategoriesFrame.Items.Add(Div);
PriceExists.IsChecked=false;
Category.Text=“Nazwa kategorii”;
}
私有无效分区(对象发送方、,
Windows.UI.Xaml.Input.RightTappedRoutedEventArgs(e)
{
//问题是科德。
如果(CategoriesFrame.SelectedItems.Count==1&&
CategoriesFrame.SelectedItems[0]。等于(发件人))
{
MainFrame.Children.Remove(MainFrame.Children.OfType().First());
}
其他的
{                
if(MainFrame.Children.OfType().SingleOrDefault()==null)
{
按钮移除=新按钮();
删除.Content=“Usuńzaznaczone kategorię”;
移除。单击+=移除\u单击;
MainFrame.Children.Add(remove);
}                
}
}
私有异步无效删除\单击(对象发送方,Windows.UI.Xaml.RoutedEventArgs e)
{
如果(CategoriesFrame.SelectedItems.Count>0)
{
Categories.RemoveAt(CategoriesCollection.IndexOf
((Grid)CategoriesFrame.SelectedItems[0]);
CategoriesCollection.Remove((网格)CategoriesFrame.SelectedItems[0]);
CategoriesFrame.Items.Remove(CategoriesFrame.SelectedItems[0]);
删除单击(此项为空);
等待序列化();
}
其他的
{
MainFrame.Children.Remove(MainFrame.Children.OfType().SingleOrDefault());
}
}
私有异步void FillGrids()
{
等待反序列化();
foreach(类别中的选项Opcja)
{
复选框Cat_active=新复选框();
Grid Div=新网格();
Div.MinWidth=100;
Cat_active.Content=“[”+Opcja.InOut+“]”
Opcja.Title+“”+Opcja.Value+“zł”;
分区儿童添加(Cat_激活);
里格区
 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);
            }                
        }
    }