황현동 블로그 개발, 인생, 유우머

160204 uwp study

Tags:




앱데이타 사용

var localSetting = ApplicationData.Current.LocalSetting;
var localFolder = ApplicationData.Current.LocalFolder;
localSettings["abc"] = "에이비씨";
var file = await localFolder.CreateFileAsync("abc.txt", CreateCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, "abc");

임시 디렉토리

var tempFolder = ApplicationData.Current.TemporaryFolder;

앱버전관리

Application.Version
ApplicationData.SetVersionAsync

클립보드 변경내용 추적

https://msdn.microsoft.com/ko-kr/library/windows/apps/mt243291.aspx

Clipboard.ContentChanged += (s, e) => 
{
    var dataPackageView = Clipboard.GetContent();
    if (dataPackageView.Contains(StandardDataFormats.Text))
    {
        var text = await dataPackageView.GetTextAsync();
        TextOutput.Text = "Clipboard now contains: " + text;
    }
}

사진촬영

https://msdn.microsoft.com/ko-kr/library/windows/apps/mt282142.aspx

var captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200); 

var photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

if (photo == null)
{
    // User cancelled photo capture
    return;
}

var stream = await photo.OpenAsync(FileAccessMode.Read);
var decoder = await BitmapDecoder.CreateAsync(stream);
var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

var softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
        BitmapPixelFormat.Bgra8, 
        BitmapAlphaMode.Premultiplied);

var bitmapSource = new SoftwareBitmapSource();
await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

imageControl.Source = bitmapSource;

UI 스레드 디스패치

https://msdn.microsoft.com/ko-kr/library/windows/apps/mt187336.aspx Windows.UI.Core.CoreDispatcher를 사용하여 업데이트를 UI 스레드로 디스패치합니다. Sleep 함수 대신 ThreadPoolTimer.CreateTimer를 사용합니다. 고유한 스레드 관리 시스템을 만드는 대신 스레드 풀을 사용합니다. 스레드 풀은 고급 접근 권한 값을 사용하여 OS 수준에서 실행되며 장치 리소스 및 활동에 따라 시스템 전체와 프로세스 내에서 동적으로 조정되도록 최적화되어 있습니다.

스레드 타이머

https://msdn.microsoft.com/ko-kr/library/windows/apps/mt187338.aspx

TimeSpan period = TimeSpan.FromSeconds(60);

ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
    {
        // 
        // TODO: Work
        // 
        
        // 
        // Update the UI thread by using the UI core dispatcher.
        // 
        Dispatcher.RunAsync(CoreDispatcherPriority.High,
            () =>
            {
                // 
                // UI components can be accessed within this scope.
                // 

            });

    }, period);

일회성 타이머

https://msdn.microsoft.com/ko-kr/library/windows/apps/mt187341.aspx

스레드풀

https://msdn.microsoft.com/ko-kr/library/windows/apps/mt187339.aspx

// The nth prime number to find.
const uint n = 9999;

// A shared pointer to the result.
// We use a shared pointer to keep the result alive until the 
// thread is done.
ulong nthPrime = 0;

// Simulates work by searching for the nth prime number. Uses a
// naive algorithm and counts 2 as the first prime number.
IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
    (workItem) =>
{
    uint  progress = 0; // For progress reporting.
    uint  primes = 0;   // Number of primes found so far.
    ulong i = 2;        // Number iterator.

    if ((n >= 0) && (n <= 2))
    {
        nthPrime = n;
        return;
    }

    while (primes < (n - 1))
    {
        if (workItem.Status == AsyncStatus.Canceled)
        {
            break;
        }

        // Go to the next number.
        i++;

        // Check for prime.
        bool prime = true;
        for (uint j = 2; j < i; ++j)
        {
            if ((i % j) == 0)
            {
                prime = false;
                break;
            }
        };

        if (prime)
        {
            // Found another prime number.
            primes++;

            // Report progress at every 10 percent.
            uint temp = progress;
            progress = (uint)(10.0*primes/n);

            if (progress != temp)
            {
                String updateString;
                updateString = "Progress to " + n + "th prime: "
                    + (10 * progress) + "%\n";

                // Update the UI thread with the CoreDispatcher.
                CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                    CoreDispatcherPriority.High,
                    new DispatchedHandler(() =>
                {
                    UpdateUI(updateString);
                }));
            }
        }
    }

    // Return the nth prime number.
    nthPrime = i;
});

// A reference to the work item is cached so that we can trigger a
// cancellation when the user presses the Cancel button.
m_workItem = asyncAction;