Home > database >  Add Click event to my dynamically created Xaml Button
Add Click event to my dynamically created Xaml Button

Time:08-08

I'm currently writing a function that creates button in XAML (on runtime) according to JSON information that is received from an API call in C#.

I currently have the following code:

Button downloadButton = new Button();
downloadButton.Click  = new RoutedEventHandler(DownloadContent);

But I get the following error:

An object reference is required for the non-static field, method, or property 'Mainwindow.DownloadContent(object, RoutedEventArgs)'

I've tried changing it to downloadButton.Click = new RoutedEventHandler(this, DownloadContent); but that gives me the error:

Keyword 'this' is not valid in a static property, static method, or static field initializer

For reference this is what DownloadContent looks like

private void DownloadContent(object sender, RoutedEventArgs e)
{
    WebClient webClient = new WebClient();
    webClient.DownloadFileAsync(new Uri("API-URL"), contentZip);
    webClient.DownloadFileCompleted  = new AsyncCompletedEventHandler(InstallContent);        
}

This is what I want the button to look like (skipping the other stuff like background color, etc.)

<Button Name="Downloadbutton" 
  Content="Download"
  Click="DownloadContent"
</Button>

Am I missing something as why I get this Error and how could I fix it?

EDIT

The Installcontent function looks like this:

private void InstallContent(object sender, AsyncCompletedEventArgs e)
{
    ZipFile.ExtractToDirectory(contentZip, contentPath, true);
    File.Delete(contentZip);
    writeJsonData(DLCbuttonTag);
    Downloadbutton.Visibility = Visibility.Collapsed;
    Uninstallbutton.Visibility = Visibility.Visible;
}

The function that creates the button:

public static void getContentList()
        {
        string jsonString = File.ReadAllText(@"C:\Users\stage\OneDrive\Documenten\ContentInfo.json")
        ContentList contentList = JsonConvert.DeserializeObject<ContentList>(jsonString);;

            for (int i = 0; i < contentList.Content.Count; i  )
            {
                    StackPanel dynamicStackPanel = new StackPanel();
                    dynamicStackPanel.Orientation = Orientation.Horizontal;
                    dynamicStackPanel.Background = Brushes.Transparent;
                    
                    Button downloadButton = new Button();
                    downloadButton.Click  = new RoutedEventHandler(DownloadContent);
                    dynamicStackPanel.Children.Add(downloadButton);
            }
        }

CodePudding user response:

The problem is that getContentList is declared as static. You try to reference the method DownloadContent which is an instance method. Remove the static from getContentList to fix the error.

The reason for the error is that static methods cannot access instance members. Static members do not run in the context of a particular instance, but in the context of the type itself. While in the case of WPF there most likely is no other instance of your page, in theory there could be many. The static method would not know which instance to pick, hence the error.

See this link for details on static members.

As a sidenote: in C#, methods are usually started with a capital letter (Pascal casing) regardless of their access modifier. So getContentList would be GetContentList.

  • Related