Presentation is loading. Please wait.

Presentation is loading. Please wait.

Разработка Windows 8 приложений на С++ Сергей Байдачный Специалист по разработке программного обеспечения Microsoft Ukraine.

Similar presentations


Presentation on theme: "Разработка Windows 8 приложений на С++ Сергей Байдачный Специалист по разработке программного обеспечения Microsoft Ukraine."— Presentation transcript:

1 Разработка Windows 8 приложений на С++ Сергей Байдачный Специалист по разработке программного обеспечения Microsoft Ukraine

2 Платформа для разработчика Windows Core OS Services JavaScript (Chakra) C C++ C# VB Windows Store Apps Communication & Data Application Model Devices & Printing WinRT APIs Graphics & Media XAML HTML / CSS HTML JavaScript C C++ C# VB Desktop Apps Win32. NET / SL Internet Explorer System Services View Model Controller Core

3 Что такое WinRT API COM

4 .winmd metadata.winmd files Contain the metadata representation of WinRT types To consume a winmd file: Right click on project in Solution Explorer > References > Add New Reference… Or #using Make sure the winmd and implementation dll is packaged together with your application To produce a.winmd file: Start from the “C++ WinRT Component Dll” template Define public types (ref classes, interfaces, delegates, etc.) Tip: C++ WinRT components can be consumed from C++, JS or C#

5 Windows 8 APIs Windows Core OS Services

6 Язык программирования С#

7 Язык программирования С++

8 C++ reimagined C++ 11 auto shared_ptr decltype..... http://msdn.microsoft.com/en-us/library/hh567368.aspx C++/CX The Windows Runtime language extensions lets you use the power and performance of C++ to build Windows 8 apps

9 C++ Component Extensions Key Bindings FeatureSummary 1. Data Types ref classReference type value classValue type interface classInterface propertyProperty with get/set event“Delegate property” with add/remove/raise delegateType-safe function pointer genericType-safe generics 2. Allocation gcnewGarbage-collected allocation ref newReference-counted allocation 3. Pointer & Reference ^Strong pointer (“hat” or “handle”) %Strong reference

10 WinRT Types: For Cross- Language Use Module Internals written in C++ Module Internals written in C++ WinRT External Surface for WinRT callers/callees C/C++ External Surface for native callers/callees

11 Runtime Class Defining public ref class Person { public: Person(String^ name, String^ email); void Greet(Person^ other); internal: ~Person(); void SetPassword(const std::wstring& passwd); }; ABI-safe cross-language class Public methods restricted to WinRT typed parameters Private/internal methods can use any legal C++ type parameters Using Person^ p = ref new Person(“John Surname”); p->Greet(ref new Person(“Jim Surename”);

12 Lifetime Management Handle (^) is a pointer to a Windows Runtime object for which the compiler performs automatic reference counting ref new instantiates or activates a Windows Runtime class. Person^ p; { Person^ p2 = ref new Person(); // refcount = 1 p2->Name = “John”; // refcount = 1 p = p2; // refcount = 2 } // refcount = 1 p = nullptr; // refcount = 0; ~Person()

13 Interface Inheriting public interface class IFeline : IAnimal { void Scratch(); }; Implementing ref class Cat : IFeline { public: virtual void Play(); virtual void Scratch(); }; Public inheritance only Defining public interface class IAnimal { void Play(); }; Methods are implicitly public Using IAnimal^ animal = ref new Cat(); animal->Play();

14 Property Defining Trivial properties (with private backing store) public: property String^ Name; User defined properties public: property Person^ Sibling { Person^ get() { InitSiblings(); return _sibling; } void set(Person^ value) { _sibling = value; NotifySibling(); } } private: Person^ _sibling; Using Person^ p = ref new Person(“John”); p->Sibling = ref new Person(p->Name);

15 Delegate Declaring: like a function public delegate void PropertyChanged( String^ propName, String^ propValue ); Instantiating: like a class From lambda: auto p = ref new PropertyChanged( [](String^ pn, String^ pv) { cout << pn << ” = “ << pv; } ); From free-function auto p = ref new PropertyChanged( UIPropertyChanged ); From class-member auto p = ref new PropertyChanged( this, MainPage::OnPropertyChanged ); Invoking: like a function p( “Visible”, false );

16 Event Defining Trivial event (with private backing store) public: event PropertyChanged^ OnPropertyChanged; User defined event public: event PropertyChanged^ OnNetworkChanged { EventRegistrationToken add(PropertyChanged^); void remove(EventRegistrationToken t); void raise(String^, String^); } Using Subscribing person->OnPropertyChanged += propertyChangedDelegate; auto token = person- >OnPropertyChanged::add(propertyChangedDelegate); Unsubscribing person->OnPropertyChanged -= token; person->OnPropertyChanged::remove(token);

17 Exception Signaling an error case: throw exception throw ref new InvalidArgumentException(); throw ref new COMException(E_*); Handling an error case: catch exception try { … } catch (OutOfMemoryException^ ex) { … } Access HRESULT value via ex->HResult Notes on exceptions: catch (Platform::Exception^) catches all WinRT exceptions Exceptions don’t carry any state and don’t travel across modules Deriving from an exception class is ill-formed HRESULTException E_OUTOFMEMORY OutOfMemoryExceptio n E_INVALIDARG InvalidArgumentExcepti on E_NOINTERFACEInvalidCastException E_POINTERNullReferenceException E_NOTIMPL NotImplementedExcept ion E_ACCESSDENIEDAccessDeniedException E_FAILFailureException E_BOUNDSOutOfBoundsException E_CHANGED_STATEChangedStateException REGDB_E_CLASSNO TREG ClassNotRegisteredExce ption E_DISCONNECTEDDisconnectedException E_ABORTOperationCanceledExce ption

18 Generics Implementing ref class PairStringUri: IPair { public: property String^ First; property Uri^ Second; }; Defining generic public interface class IPair { property T First; property U Second; }; Using IPair ^ uri = GetUri(); auto first = uri->First; // type is String^ auto second = uri->Second; // type is Uri^

19 Partial Runtime Class Partial class definition private partial ref class MainPage: UserControl, IComponentConnector { public: void InitializeComponent(); void Connect() { btn1->Click += ref new EventHandler(this, &MainPage::Button_Click); } }; Class definition ref class MainPage { public: MainPage() { InitializeComponent(); } void Button_Click(Object^ sender, RoutedEventArgs^ e); };

20 Нужно ли мне выбирать С++?

21 Вопросы?


Download ppt "Разработка Windows 8 приложений на С++ Сергей Байдачный Специалист по разработке программного обеспечения Microsoft Ukraine."

Similar presentations


Ads by Google