Skip to content

Commit b8f7381

Browse files
Merge pull request #6507 from MicrosoftDocs/jken/winui3-tutorial-fix
Fix C++/WinRT PropertyChanged code in WinUI 3 photo viewer tutorial
2 parents be1b484 + 04d6fe5 commit b8f7381

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

hub/apps/get-started/simple-photo-viewer-winui3.md

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Tutorial--Create a simple photo viewer with WinUI
33
description: In this topic we walk through the process of building a simple WinUI app to display photos. We'll use controls, layout panels, and data-binding. And we'll be writing both XAML markup (which is *declarative*) and C# code (which is *imperative*, or *procedural*).
44
ms.topic: tutorial
5-
ms.date: 08/19/2024
5+
ms.date: 03/24/2026
66
keywords: Windows, App, SDK, WinUI, WinUI, photo, viewer, Windows 11, Windows 10, XAML, C#, C++
77
ms.localizationpriority: medium
88
---
@@ -238,6 +238,9 @@ A *model* (in the sense of models, views, and view models) is a class that to so
238238

239239
1. Replace the contents of `ImageFileInfo.h` with the code listing below.
240240

241+
> [!TIP]
242+
> For a complete, buildable reference implementation of this class, see [Photo.h](https://github.com/microsoft/WindowsAppSDK-Samples/blob/main/Samples/PhotoEditor/cpp-winui/PhotoEditor/Photo.h) in the Windows App SDK Photo Editor C++ sample.
243+
241244
```cppwinrt
242245
// ImageFileInfo.h
243246
#pragma once
@@ -250,17 +253,17 @@ A *model* (in the sense of models, views, and view models) is a class that to so
250253
ImageFileInfo() = default;
251254

252255
ImageFileInfo(
253-
winrt::Windows::Storage::FileProperties::ImageProperties const& properties,
254-
winrt::Windows::Storage::StorageFile const& imageFile,
256+
Windows::Storage::FileProperties::ImageProperties const& properties,
257+
Windows::Storage::StorageFile const& imageFile,
255258
hstring const& name,
256259
hstring const& type);
257260

258-
winrt::Windows::Storage::FileProperties::ImageProperties ImageProperties()
261+
Windows::Storage::FileProperties::ImageProperties ImageProperties()
259262
{
260263
return m_imageProperties;
261264
}
262265

263-
winrt::Windows::Storage::StorageFile ImageFile()
266+
Windows::Storage::StorageFile ImageFile()
264267
{
265268
return m_imageFile;
266269
}
@@ -294,15 +297,8 @@ A *model* (in the sense of models, views, and view models) is a class that to so
294297

295298
void ImageRating(uint32_t value);
296299

297-
winrt::event_token PropertyChanged(winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
298-
{
299-
return m_propertyChanged.add(handler);
300-
}
301-
302-
void PropertyChanged(winrt::event_token const& token) noexcept
303-
{
304-
m_propertyChanged.remove(token);
305-
}
300+
event_token PropertyChanged(Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
301+
void PropertyChanged(event_token const& token) noexcept;
306302

307303
Windows::Foundation::IAsyncOperation<Microsoft::UI::Xaml::Media::Imaging::BitmapImage> GetImageSourceAsync();
308304

@@ -316,10 +312,7 @@ A *model* (in the sense of models, views, and view models) is a class that to so
316312
hstring m_imageFileType;
317313

318314
event<Microsoft::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
319-
void OnPropertyChanged(hstring propertyName)
320-
{
321-
m_propertyChanged(*this, Microsoft::UI::Xaml::Data::PropertyChangedEventArgs(propertyName));
322-
}
315+
void RaisePropertyChanged(hstring const& propertyName);
323316
};
324317
}
325318
namespace winrt::SimplePhotos::factory_implementation
@@ -343,6 +336,7 @@ A *model* (in the sense of models, views, and view models) is a class that to so
343336
namespace winrt
344337
{
345338
using namespace Microsoft::UI::Xaml;
339+
using namespace Microsoft::UI::Xaml::Data;
346340
using namespace Microsoft::UI::Xaml::Media::Imaging;
347341
using namespace Windows::Foundation;
348342
using namespace Windows::Storage;
@@ -353,8 +347,8 @@ A *model* (in the sense of models, views, and view models) is a class that to so
353347
namespace winrt::SimplePhotos::implementation
354348
{
355349
ImageFileInfo::ImageFileInfo(
356-
winrt::Windows::Storage::FileProperties::ImageProperties const& properties,
357-
winrt::Windows::Storage::StorageFile const& imageFile,
350+
Windows::Storage::FileProperties::ImageProperties const& properties,
351+
Windows::Storage::StorageFile const& imageFile,
358352
hstring const& name,
359353
hstring const& type) :
360354
m_imageProperties{ properties },
@@ -368,13 +362,28 @@ A *model* (in the sense of models, views, and view models) is a class that to so
368362
ImageRating(rating == 0 ? dist(random) : rating);
369363
}
370364

365+
event_token ImageFileInfo::PropertyChanged(Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
366+
{
367+
return m_propertyChanged.add(handler);
368+
}
369+
370+
void ImageFileInfo::PropertyChanged(event_token const& token) noexcept
371+
{
372+
m_propertyChanged.remove(token);
373+
}
374+
375+
void ImageFileInfo::RaisePropertyChanged(hstring const& propertyName)
376+
{
377+
m_propertyChanged(*this, PropertyChangedEventArgs{ propertyName });
378+
}
379+
371380
void ImageFileInfo::ImageTitle(hstring const& value)
372381
{
373382
if (ImageProperties().Title() != value)
374383
{
375384
ImageProperties().Title(value);
376385
ImageProperties().SavePropertiesAsync();
377-
OnPropertyChanged(L"ImageTitle");
386+
RaisePropertyChanged(L"ImageTitle");
378387
}
379388
}
380389

@@ -384,7 +393,7 @@ A *model* (in the sense of models, views, and view models) is a class that to so
384393
{
385394
ImageProperties().Rating(value);
386395
ImageProperties().SavePropertiesAsync();
387-
OnPropertyChanged(L"ImageRating");
396+
RaisePropertyChanged(L"ImageRating");
388397
}
389398
}
390399

0 commit comments

Comments
 (0)