2

i would like to get the DisplayName of the package in C++/WinRT.

As describe in this C# example:

<TextBlock x:Name="AppTitle"
                       Text="{x:Bind GetAppTitleFromSystem()}"
                       VerticalAlignment="Center"
                       Style="{StaticResource CaptionTextBlockStyle}" />
namespace AppUIBasics
{
    public sealed partial class NavigationRootPage : Page
    {
        public string GetAppTitleFromSystem()
        {
            return Windows.ApplicationModel.Package.Current.DisplayName;
        }
    }
}

i tried to :

// .xaml.h file
struct MainWindow : MainWindowT<MainWindow>
{
        MainWindow();
        winrt::hstring GetAppTitleFromSystem();
}
...
// .xaml.cpp file
...
MainWindow::MainWindow()
{
        InitializeComponent();        
}
hstring MainWindow::GetAppTitleFromSystem()
{
        Package package = Package::Current();
        return package.DisplayName();
}
...
// .idl
namespace Example
{
    [default_interface]
    runtimeclass MainWindow : Microsoft.UI.Xaml.Window
    {
        MainWindow();

        String GetAppTitleFromSystem{ get; };        
    }
}

i got error like:

2.Error XLS0525 Method 'GetAppTitleFromSystem' not found in type 'MainWindow. Example C:\Users\{..}\MainWindow.xaml 72

  • `Windows.ApplicationModel` is a namespace, not a type. That's why the IDL compiler complains. – IInspectable Mar 19 '22 at 18:13
  • THank you my bad! i add the namespace `using namespace Windows::ApplicationModel;` but i'm still getting `error WMC1110: Invalid binding path 'GetAppTitleFromSystem()' : Expecting a method.`. So could i resolve it? – LegitLearner Mar 20 '22 at 00:40
  • Making changes to your C++ code won't retroactively fix your IDL. The IDL compiler is the first thing that runs, and if it fails, any subsequent error diagnostic is possible. You'll have to fix the IDL first, and only then move forward. – IInspectable Mar 20 '22 at 08:08
  • Yes, as said the first error from IDL has been solved! – LegitLearner Mar 20 '22 at 08:40
  • 2
    If you're binding a property, the XAML expression doesn't use function call syntax. In other words, use `Text="{x:Bind GetAppTitleFromSystem}"` instead. I don't know whether that fixes things for you. The question has been edited into a state where the problem is partially solved and partially isn't, making it hard to see what the problem is. – IInspectable Mar 20 '22 at 09:01

0 Answers0