I am using UWP to create an app for a server application and I have made a class serverclass.h in c++ in which I have a string output. Now I want to print this output in a textbox in xaml. I have attached the codes below. When I'm using this->DataContext=ser; it is giving me an error:"function Windows::UI::Xaml::FrameworkElement::DataContext::set cannot be called with the given argument list". What is the problem here?
mainpage.xaml.cpp
serverclass ser;
MainPage::MainPage()
{
InitializeComponent();
this->DataContext = ser;
}
void App1::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
ser.output = "hey";
ser.connect();
}
serverclass.h
class serverclass
{
public:
string output;
}
"mainpage.xaml"
<TextBox Text="{Binding output}" Height="50" FontSize="30px">
</TextBox>
If you want to print your string output in a textbox in xaml, you could use data binding referring to the document and sample.
For the scenario mentioned by you, you could check the following code: serverclass.h
namespace YourAppName{//Put the serverclass into the namespace
public ref class serverclass sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
{
private:
Platform::String^ output;
public:
virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;
serverclass()
{ }
property Platform::String^ Output {
Platform::String^ get() { return output; }
void set(Platform::String^ value) {
if (output != value)
{
output = value;
PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs("Output"));
}
}
};
};
}
MainPage.xaml.h
public ref class MainPage sealed
{
public:
MainPage();
property serverclass^ Ser {
serverclass^ get() { return ser; }
void set(serverclass^ value) {
ser = value;
}
}
private:
serverclass^ ser;
void Button_Click(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e);
};
MainPage.xaml.cpp
MainPage::MainPage()
{
InitializeComponent();
this->ser = ref new serverclass();
ser->Output = "world";
this->DataContext = ser;
}
void DataContextSample::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
ser->Output = "hello";
}
MainPage.xaml
<StackPanel>
<TextBox Text="{x:Bind Ser.Output,Mode=TwoWay}" Width="100" Height="30" Margin="10"/>
<Button Click="Button_Click" Content="click me"/>
</StackPanel>