Home > Blockchain >  I need to use a string in multiple .cs files for a ContentDialog box
I need to use a string in multiple .cs files for a ContentDialog box

Time:08-17

I've been having an issue that I'm stuck on. I can't seem to figure out how to use a string in multiple .cs files. I'm writing a task list app in WinUI 3 C#, and I have a control called "ContentDialog". This control has 2 separate files, ContentDialogContent.xaml, and ContentDialogContent.xaml.cs. This is due to how this control is made. Anyway, I have a TextBox inside of it to enter a task. I need to pull the string from that TextBox, save it after the "Add" button is clicked, then use it in another file, TasksPage.xaml.cs. Here's an image representation of what I need to do: Image link because I can't post images yet :)

CodePudding user response:

I've fixed it. I ended up rewriting the whole ContentDialog, making it fully separate from the main page, then I set up a tag in the dialog class to call it like "dialog.Tag". Took some time but I got it :)

CodePudding user response:

Let me suggest you another way to do this.

TaskPage.xaml

<Grid>
    <Button
        x:Name="AddTaskButton"
        Click="AddTaskButton_Click"
        Content="Add task" />
    <ContentDialog
        x:Name="TaskContentDialog"
        CloseButtonText="Cancel"
        PrimaryButtonText="Add">
        <TextBox x:Name="TaskContentDialogTextBox" PlaceholderText="Enter task" />
    </ContentDialog>
</Grid>

TaskPage.xaml.cs

public sealed partial class TaskPage : Page
{
    public TaskPage()
    {
        this.InitializeComponent();
    }

    public string? TaskString { get; set; }

    private async void AddTaskButton_Click(object sender, RoutedEventArgs e)
    {
        if (await this.TaskContentDialog.ShowAsync() is ContentDialogResult.Primary)
        {
            TaskString = this.TaskContentDialogTextBox.Text;
        }
    }
}
  • Related