2017년 1월 31일 화요일

자마린 스튜디오(Xamarin Studio)에서 비동기로 이미지 받아오기

이미지의 경우 속도가 느리기 때문에 대부분 비동기 형태로 웹서버에서 받아와야 합니다.
iOS의 경우 GCD를 제공합니다. 닷넷의 경우 await/async를 결합해서 보다 쉽게 사용할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:DemoBacktoWeb" x:Class="DemoBacktoWeb.DemoBacktoWebPage">

     <ContentPage.Padding>
         <OnPlatform x:TypeArguments="Thickness"
             iOS="0, 20, 0, 0" />
        </ContentPage.Padding>
    
      <StackLayout>
         <Grid VerticalOptions="FillAndExpand">
             <Label x:Name="errorLabel"
                 HorizontalOptions="Center"
                 VerticalOptions="Center" />
             <ActivityIndicator IsRunning="{Binding Source={x:Reference image},
                 Path=IsLoading}" />
             <Image x:Name="image" />
         </Grid>
        
         <Button Text="Load Bitmap"
             HorizontalOptions="Center"
             Clicked="OnLoadButtonClicked" />
     </StackLayout>
</ContentPage>


using System.Net;

namespace DemoBacktoWeb
{
    public partial class DemoBacktoWebPage : ContentPage
    {
        public DemoBacktoWebPage()
        {
            InitializeComponent();
        }

        async void OnLoadButtonClicked(object sender, EventArgs args)
        {
            try
            {
                Stream stream =
                    await GetStreamAsync("https://developer.xamarin.com/demo/IMG_1996.JPG");
                image.Source = ImageSource.FromStream(() => stream);
            }
            catch (Exception exc)
            {
                errorLabel.Text = exc.Message;
            }
        }

        async Task<Stream> GetStreamAsync(string uri)
        {
            TaskFactory factory = new TaskFactory();
            WebRequest request = WebRequest.Create(uri);
            WebResponse response = await factory.FromAsync<WebResponse>(request.BeginGetResponse,
                                                                        request.EndGetResponse, null);
            return response.GetResponseStream(); 
        }
    }
}


개발자를 위한 에저 과정(MOC)가 나왔습니다.


오랜만에 MCT 다운로드 사이트에 들어갔더니 Introduction to Azure for Developers라는 과정이 올라왔습니다. 모바일 앱에서 필요한 부분이 클라우드에 REST기반의 웹서비스를 올리고 연동하는 부분이 많은데 연습하기에 좋은 과정인 것 같습니다. 


Module 1: Overview of Microsoft Azure

Module 2: Creating and Managing Services in Azure

Module 3: Managing Infrastructure in Azure

Module 4: Creating Web Sites using Azure

Module 5: Creating Modular Web Applications using Azure

Module 6: Processing Background Logic using Azure

 Module 7: Storing SQL Data in Azure

Module 8: Storing Non-Relational Data in Azure

Module 9: Storing Files and Media in Azure

Module 10: Storing Data in Queues using Azure

Module 11: Relaying Web Services using Azure

Module 12: Asynchronously Notifying Applications using Azure


자마린 스튜디오(Xamarin Studio)에서 비동기로 얼럿 코드를 실행하기

C#을 계속 사용했던 개발자들에게는 Task클래스가 익숙합니다. 아래의 코드를 보면 태스크를 통해 경고 메세지 결과를 처리하는 코드가 보입니다. 

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:DemoAlertCallback" x:Class="DemoAlertCallback.DemoAlertCallbackPage">

    <StackLayout>
        <Button Text="Invoke Alert"
                FontSize="Large"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"
                Clicked="OnButtonClicked" />
        <Label x:Name="label"
                Text="Tap button to invoke alert"
                FontSize="Large"
                HorizontalTextAlignment="Center"
                VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>

namespace DemoAlertCallback
{
    public partial class DemoAlertCallbackPage : ContentPage
    {
        bool result; 

        public DemoAlertCallbackPage()
        {
            InitializeComponent();
        }

        void OnButtonClicked(object sender, EventArgs args)
        {
            Task<bool> task = DisplayAlert("Simple Alert",
                                           "Decide on an option",
                                           "yes or ok", "no or cancel");
            task.ContinueWith(AlertDismissedCallback);
            label.Text = "Alert is currently displayed"; 
        }
        void AlertDismissedCallback(Task<bool> task)
        {
            result = task.Result;
            Device.BeginInvokeOnMainThread(DisplayResultCallback); 
        }
        void DisplayResultCallback()
        {
            label.Text = String.Format("Alert {0} button was pressed",
                                       result ? "OK" : "Cancel"); 
        }
    }
}


실행해서 버튼을 클릭하면 결과를 확인할 수 있다.


AI 딜레마 1부 - 일자리 종말론 - PD수첩 영상입니다.

  IT분야에서 강의를 하고 있지만 개발자와 강사의 숫자가 앞으로 급격하게 줄어들 것이라는 생각을 많이 하고 있습니다. 코로나블루가 아닌 AI블루가 업계에 충격을 주고 있습니다. ㅠㅠ ​ 영상을 보다 보면 AI의 영향을 받지 않는 분야가 있을까? 하는...