翻译C#code。与代表去渣,与Android使用 [英] Translating C# code with delegate to Java, to use with Android

查看:154
本文介绍了翻译C#code。与代表去渣,与Android使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在潜水的过程中进入Android开发。最让我参与了项目,均在C#。代表们的C#我也很经常使用的元素,我也用这样的东西用一个扩展的EventArgs或属性与set和get类传递数据。随着我的编程知识,我认为我将能够得到顺利开始使用Android开发pretty。事情是我完全地不知道如何处理类似于Java C#delagte的执行机制。

下面我present,在C#中工作得很好,并且包含的​​,我想在我的未来的Andr​​oid项目中使用C#语言的一些元素一些示范课。有人可以提供我这个code的翻译?我想preFER那是我自己的例子,它的转换工作COS,让我赶上这一切得更快。此外,在主题的任何有价值的资源(不仅是代表,但转换C#转换成Java的genereal话题)将是preciated。

CountdownTimer.cs

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;命名空间SampleDelegateApp
{
    公共类CountdownTimer
    {
        定时器tmrTicks =新的Timer();
        INT secondsLeft = 0;
        INT numberOfSecondsToCountdown = 0;        公共BOOL IsWorking
        {
            {返回tmrTicks.Enabled; }
        }        公共CountdownTimer(INT秒)
        {
            如果(secondsLeft℃,)secondsLeft = 0;
            numberOfSecondsToCountdown =秒;
            secondsLeft =秒;            tmrTicks.Interval = 1000;
            tmrTicks.Tick + =新的EventHandler(tmrTicks_Tick);
            tmrTicks.Enabled = FALSE;
        }        无效tmrTicks_Tick(对象发件人,EventArgs的发送)
        {
            secondsLeft--;
            如果(secondsLeft> = 1)
                WhenCountdownTimerTick(新CountdownTimerEventArgs(secondsLeft,numberOfSecondsToCountdown,FALSE));
            其他
            {
                停止();
                WhenCountdownTimerTick(新CountdownTimerEventArgs(secondsLeft,numberOfSecondsToCountdown,真实));
            }
        }        公共无效复位()
        {
            停止();
            secondsLeft = numberOfSecondsToCountdown;
            如果(secondsLeft℃,)secondsLeft = 0;
            WhenCountdownTimerTick(新CountdownTimerEventArgs(secondsLeft,numberOfSecondsToCountdown,FALSE));
        }        公共无效停止()
        {
            tmrTicks.Enabled = FALSE;
        }        公共无效启动()
        {
            如果(secondsLeft&下; = 0)
            {
                secondsLeft = 0;
                WhenCountdownTimerTick(新CountdownTimerEventArgs(secondsLeft,numberOfSecondsToCountdown,真实));
            }
            其他
            {
                tmrTicks.Enabled = TRUE;
            }
        }        公共委托无效CountdownTimerTickEventHandler(对象发件人,CountdownTimerEventArgs EA);        公共事件CountdownTimerTickEventHandler CountdownTimerTick;        受保护的虚拟无效WhenCountdownTimerTick(CountdownTimerEventArgs EA)
        {
            如果(CountdownTimerTick!= NULL)
            {
                CountdownTimerTick(这一点,EA);
            }
        }
    }    公共类CountdownTimerEventArgs:EventArgs的
    {        公共字符串TIMESTRING =;
        公众持股量procentOfTimeLeft = 0.0;
        公共BOOL countdownFinished = FALSE;        公共CountdownTimerEventArgs(INT secondsLeft,诠释SecondsToCountdown,布尔isfinished)
        {
            countdownFinished = isfinished;
            TIMESTRING =的String.Format({} 0:00:{} 1:00,secondsLeft / 60,secondsLeft 60%);
        }
    }
}

frmTest.cs

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;命名空间SampleDelegateApp
{
    公共部分类frmTest:表单
    {
        CountdownTimer ctmrTest;        公共frmTest()
        {
            的InitializeComponent();
            ctmrTest =新CountdownTimer(-44);
            ctmrTest.CountdownTimerTick + =新CountdownTimer.CountdownTimerTickEventHandler(ctmrTest_CountdownTimerTick);
        }        无效ctmrTest_CountdownTimerTick(对象发件人,CountdownTimerEventArgs EA)
        {
            lblTimer.Text = ea.timeString;
            如果(ea.countdownFinished)countdownEnd();
        }        私人无效btnStart_Click(对象发件人,EventArgs的发送)
        {
            ctmrTest.Reset();
            ctmrTest.Start();
        }        无效countdownEnd()
        {
            的MessageBox.show(完成);
        }
    }
}


解决方案

我明显地使用一些词在我的问题,使得它所有的不明确和/或谁试图帮助我完全地missunderstood我需要为这个问题的人

。把单词代表在这里和那里使它更复合。后一个问题在其他的方式问我有我的答案,让我能回答我的问题:

<一个href=\"http://stackoverflow.com/questions/13262539/how-to-achieve-following-c-sharp-behaviour-in-java-android\">How实现以下在Java(Android版)C#的行为

我不是说低于code是达到最好的编码标准,例如与这里显示了在现实生活中项目的任何使用,但是这正是我一直在寻找。下面的例子:

tester.java

 公共类测试仪{
    公共静态无效的主要(字串[] args){
        测试测试1 =新的测试();
        test1.start();
    }
}

test.java

 公共类测试实现CountdownTextTickListener {    公开测试(){}    公共无效的start(){
        CountdownText CTX =新CountdownText(100);
        ctx.setListener(本);
        ctx.Start();
    }    @覆盖
    公共无效CountdownTextTickEventFired(对象发件人,
                        CountdownTextTickEventArgs EventArgs的){
            的System.out.println(eventArgs.TimeString);
            如果(eventArgs.isStopped)的System.out.println( - 完 - );
    }
}

CountdownTextTickListener.java

 公共接口CountdownTextTickListener {
     无效CountdownTextTickEventFired(对象发件人,EventArgs的CountdownTextTickEventArgs);
}

CountdownTextTickEventArgs.java

 公共类CountdownTextTickEventArgs {    公共字符串TimeString以=;
    公共布尔isStopped = FALSE;    公共CountdownTextTickEventArgs(INT秒,布尔isStoppedState){
        TimeString以=的String.format(%02D:%02D秒/ 60秒,60%);
        isStopped = isStoppedState;
    }}

CountdownText.java

 进口java.util.Timer中;
进口java.util.TimerTask中;公共类CountdownText {    定时器tmrTicks =新的Timer();
    INT secondsLeft = 0;
    INT numberOfSecondsToCountdown = 0;
    布尔isWorking = FALSE;
    私人CountdownTextTickListener监听= NULL;    公共布尔getIsWorking(){
        返回isWorking;
    }    公共CountdownText(INT秒){
        如果(secondsLeft℃,)secondsLeft = 0;
        numberOfSecondsToCountdown =秒;
        secondsLeft =秒;
    }    无效startTimer所(){
        isWorking =真;
        fireEvent(secondsLeft,FALSE);
        tmrTicks =新的Timer();
        tmrTicks.scheduleAtFixedRate(新的TimerTask(){
            @覆盖
            公共无效的run(){
                tickTimer();
            }
        },1000,1000);
    }    私人无效stopTimer(){
        isWorking = FALSE;
        tmrTicks.cancel();
    }    私人无效tickTimer(){
         secondsLeft--;
         如果(secondsLeft&GT; = 1)
         {
             fireEvent(secondsLeft,FALSE);
         }
         其他
         {
             停止();
             fireEvent(secondsLeft,真);
         }
    }    公共无效复位(){
        停止();
        secondsLeft = numberOfSecondsToCountdown;
        fireEvent(secondsLeft,FALSE);
    }    公共无效停止(){
        stopTimer();
    }    公共无效启动(){
        如果(secondsLeft&下; = 0)
        {
            secondsLeft = 0;
            fireEvent(secondsLeft,真);
        }
        其他
        {
            startTimer所();
        }
    }    保护无效fireEvent(INT秒,布尔isStoppedState){
        如果(听众!= NULL){
            对象发件人=这一点;
            CountdownTextTickEventArgs的EventArgs =新CountdownTextTickEventArgs(秒,isStoppedState);
            listener.CountdownTextTickEventFired(发件人,EventArgs的);
        }
    }    公共无效使用setListener(CountdownTextTickListener监听){
        this.listener =侦听器;
    }
}

I'm in the process of diving into Android development. Most of the projects I was involved with, were in C#. Delegates were elements of C# I did use very often, I've used also stuff like passing data using class that extends EventArgs or properties with set and get. With my programming knowledge I think I will be able to get started with Android development pretty smoothly. The thing is I have completly no idea how to approach an implementing mechanism similar to C# delagte in Java.

Below I present some exemplary class that works in C# just fine and contains some elements of C# language that I would like to use in my future Android projects. Can someone provide me with a translation of this code? I would prefer that 'cos working with my own example and its conversion would allow me to catch it all faster. Also, any valuable resources on the topic (not only delegates but genereal topic of converting C# into Java) would be apreciated.

CountdownTimer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleDelegateApp
{
    public class CountdownTimer
    {
        Timer tmrTicks = new Timer();
        int secondsLeft = 0;
        int numberOfSecondsToCountdown = 0;

        public bool IsWorking
        {
            get { return tmrTicks.Enabled; }
        }

        public CountdownTimer(int seconds)
        {
            if (secondsLeft < 0) secondsLeft = 0; 
            numberOfSecondsToCountdown = seconds;
            secondsLeft = seconds;

            tmrTicks.Interval = 1000;
            tmrTicks.Tick += new EventHandler(tmrTicks_Tick);
            tmrTicks.Enabled = false;
        }

        void tmrTicks_Tick(object sender, EventArgs e)
        {
            secondsLeft--;
            if (secondsLeft >= 1) 
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
            else
            {
                Stop();
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
        }

        public void Reset()
        {
            Stop();
            secondsLeft = numberOfSecondsToCountdown;
            if (secondsLeft < 0) secondsLeft = 0;
            WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false));
        }

        public void Stop()
        {
            tmrTicks.Enabled = false;
        }

        public void Start()
        {
            if (secondsLeft <= 0)
            {
                secondsLeft = 0;
                WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true));
            }
            else
            {
                tmrTicks.Enabled = true;
            }
        }

        public delegate void CountdownTimerTickEventHandler(object sender, CountdownTimerEventArgs ea);

        public event CountdownTimerTickEventHandler CountdownTimerTick;

        protected virtual void WhenCountdownTimerTick(CountdownTimerEventArgs ea)
        {
            if (CountdownTimerTick != null)
            {
                CountdownTimerTick(this, ea);
            }
        }
    }

    public class CountdownTimerEventArgs : EventArgs
    {

        public string timeString = "";
        public float procentOfTimeLeft = 0.0f;
        public bool countdownFinished = false;

        public CountdownTimerEventArgs(int secondsLeft, int SecondsToCountdown, bool isfinished)
        {
            countdownFinished = isfinished;
            timeString = string.Format("{0:00}:{1:00}", secondsLeft / 60, secondsLeft % 60);
        }
    }
}

frmTest.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleDelegateApp
{
    public partial class frmTest : Form
    {
        CountdownTimer ctmrTest;

        public frmTest()
        {
            InitializeComponent();
            ctmrTest = new CountdownTimer(-44);
            ctmrTest.CountdownTimerTick += new CountdownTimer.CountdownTimerTickEventHandler(ctmrTest_CountdownTimerTick); 
        }

        void ctmrTest_CountdownTimerTick(object sender, CountdownTimerEventArgs ea)
        {
            lblTimer.Text = ea.timeString;
            if (ea.countdownFinished) countdownEnd();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ctmrTest.Reset();
            ctmrTest.Start();
        }

        void countdownEnd()
        {
            MessageBox.Show("Finish");
        }
    }
}

解决方案

I apparently used some words in my question that made it all unclear and/or persons who tried to help me completly missunderstood my needs as for this question. Putting word "delegate" here and there made it even more complexed. After one more question asked in other way i got my answer that made me able to answer my question:

How to achieve following c# behaviour in java (Android)

I'm not saying that code below is up to the best coding standards, and example shown here got any use in real life project, but this is exactly what i was looking for. Below working example:

tester.java

public class tester{
    public static void main(String[] args) {
        test test1 = new test(); 
        test1.start();
    }
}

test.java

public class test implements CountdownTextTickListener {

    public test() { }

    public void start() {
        CountdownText ctx = new CountdownText(100);
        ctx.setListener(this);
        ctx.Start();
    }

    @Override
    public void CountdownTextTickEventFired(Object sender, 
                        CountdownTextTickEventArgs eventArgs) {
            System.out.println(eventArgs.TimeString);
            if(eventArgs.isStopped) System.out.println("- END -");
    }
}

CountdownTextTickListener.java

public interface CountdownTextTickListener {
     void CountdownTextTickEventFired(Object sender, CountdownTextTickEventArgs eventArgs);
}

CountdownTextTickEventArgs.java

public class CountdownTextTickEventArgs {

    public String TimeString = "";
    public boolean isStopped = false;

    public CountdownTextTickEventArgs(int seconds, boolean isStoppedState) {
        TimeString = String.format("%02d:%02d",seconds/60, seconds % 60);
        isStopped = isStoppedState;
    }

}

CountdownText.java

import java.util.Timer;
import java.util.TimerTask;

public class CountdownText {

    Timer tmrTicks = new Timer();
    int secondsLeft = 0;
    int numberOfSecondsToCountdown = 0;
    boolean isWorking = false;
    private CountdownTextTickListener listener = null;

    public boolean getIsWorking(){
        return isWorking;
    }

    public CountdownText(int seconds) {
        if (secondsLeft < 0) secondsLeft = 0; 
        numberOfSecondsToCountdown = seconds;
        secondsLeft = seconds;
    }

    void startTimer() {
        isWorking = true;
        fireEvent(secondsLeft, false);
        tmrTicks = new Timer();
        tmrTicks.scheduleAtFixedRate( new TimerTask(){
            @Override
            public void run(){
                tickTimer();
            }
        }, 1000, 1000); 
    }

    private void stopTimer() {
        isWorking = false;
        tmrTicks.cancel();
    }

    private void tickTimer() {
         secondsLeft--;
         if (secondsLeft >= 1) 
         {
             fireEvent(secondsLeft, false);
         }
         else
         {
             Stop();
             fireEvent(secondsLeft, true);
         }
    }

    public void Reset() {
        Stop();
        secondsLeft = numberOfSecondsToCountdown;
        fireEvent(secondsLeft, false);
    }

    public void Stop() {
        stopTimer();
    }

    public void Start() {
        if (secondsLeft <= 0)
        {
            secondsLeft = 0;
            fireEvent(secondsLeft, true);
        }
        else
        {
            startTimer();
        }
    }

    protected void fireEvent(int seconds, boolean isStoppedState) {
        if (listener != null) {
            Object sender = this; 
            CountdownTextTickEventArgs eventArgs = new CountdownTextTickEventArgs(seconds, isStoppedState);
            listener.CountdownTextTickEventFired(sender, eventArgs);
        }
    }

    public void setListener(CountdownTextTickListener listener) {
        this.listener = listener;
    }
}

这篇关于翻译C#code。与代表去渣,与Android使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆